CareerCup Fork Problem
2014-02-23 00:09
288 查看
How many times “Hello World” is printed by following program?
int main()
{
if(fork() && fork())
{
fork();
}
if(fork() || fork())
{
fork();
}
printf(“Hello world”);
return 0;
}
a. 16
b. 20
c. 24
d. 64
----------------------------------------------------------------
b.20
----------------------------------------------------------------
pid_t fork( void);
(pid_t 是一个宏定义,其实质是int 被定义在#include<sys/types.h>中)
返回值: 若成功调用一次则返回两个值,子进程返回0,父进程返回子进程ID;否则,出错返回-1
because:
The result is 1 Hello World. Only the parent process could print and the child process couldn't print.
The result is 2 Hello Worlds, the child process will execute the fork after ||, but for && the child process will start after printf. Since the parent process get an ID and child process get an 0.
The result is 3 Hello World.
7 Hello worlds!
int main()
{
if(fork() && fork())
{
fork();
}
if(fork() || fork())
{
fork();
}
printf(“Hello world”);
return 0;
}
a. 16
b. 20
c. 24
d. 64
----------------------------------------------------------------
b.20
----------------------------------------------------------------
pid_t fork( void);
(pid_t 是一个宏定义,其实质是int 被定义在#include<sys/types.h>中)
返回值: 若成功调用一次则返回两个值,子进程返回0,父进程返回子进程ID;否则,出错返回-1
because:
#include <stdio.h> #include <unistd.h> using namespace std; int main() { if (fork() && fork()) printf("Hello World\n"); return 0; }
The result is 1 Hello World. Only the parent process could print and the child process couldn't print.
#include <stdio.h> #include <unistd.h> using namespace std; int main() { if (fork() || fork()) printf("Hello World\n"); return 0; }
The result is 2 Hello Worlds, the child process will execute the fork after ||, but for && the child process will start after printf. Since the parent process get an ID and child process get an 0.
#include <stdio.h> #include <unistd.h> using namespace std; int main() { if (fork() || fork() || fork()) printf("Hello World\n"); return 0; }
The result is 3 Hello World.
#include <stdio.h> #include <unistd.h> using namespace std; int main() { if (fork() || fork() || fork()) fork(); printf("Hello World\n"); return 0; }
7 Hello worlds!
相关文章推荐
- CareerCup Josephus Problem
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- Arrays_Strings 判断字符串中的字符是否唯一@CareerCup
- Arrays_Strings 把一个矩阵置为0 @CareerCup
- LinkLists 检查一个链表是否为回文 Check if a linked list is palindrome @CareerCup
- Tree_Graph 有序数组转BST @CareerCup
- CareerCup Fermat point of a traingle
- CareerCup之1.7 Set Matrix Zeroes
- CareerCup1.1
- 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
- careercup-C和C++ 13.9
- Moderate 找最小排序区间 @CareerCup
- Hard 找到由其它单词组成的最长单词 @CareerCup
- CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters
- Arrays_Strings 利用isSubstring检查是否子串 @CareerCup
- Tree_Graph Validate Binary Search Tree 检测一个BST是否有效 @CareerCup
- CareerCup How to find medium of 1 billion numbers across N distributed machines efficiently?
- CareerCup Old cell-phones game "snake"
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring
- CareerCup chapter 2 Linked Lists