#include <stdio.h>
#include <sys/type.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
pid_t pid;
int i;
for(i=0; i<3; i++) {
pid = fork();
if(pid == -1) {
printf("Fork Error.\n");
} else if(pid == 0) {
printf("I am child");
}
}
if(pid != 0) {
while((pid = waitpid(-1, NULL, 0)) > 0)
if(errno == ECHILD)
break;
printf("I am parent and all children have exited.\n");
}
exit(0);
return 0;
}
The result is that,
'I am child' is printed 7 times,
'I am parent and all children have exited.' is printed 4 times
and the print sequence is not fixed.
Here's my question!
Why 'I am child' is printed 7 times,
and 'I am parent and all children have exited.' is printed 4 times ?
I don't understand the number of times those sentences are printed.
Could you explain it in detail?
You can try the code below. You need to add the wait header. At the same time, after providing the 0 condition of the child processes, you should definitely sign out, otherwise each processes will fork again and again each time in the code. Shortly you have to kill every process after their task end.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(void){
pid_t pid;
int i;
for (i = 0; i < 3; i++)
{
pid = fork();
if (pid == -1) {
printf("Fork Error.\n");
} else if (pid == 0) {
printf("I am child\n");
exit(0); // !
}
}
if (pid != 0) {
while ((pid = waitpid(-1, NULL, 0)) > 0)
if (errno == ECHILD)
break;
printf("I am parent and all children have exited.\n");
}
return 0;
}
Related
The program creates a child proccess and the child does the proper work in order to see if its pid is even or odd
And for some reason even if the pid is even i get that it is odd , also the parent waits for child
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
pid_t pid, mypid, cpid;
int flag;
pid = getpid();
printf("Process %d creates new process\n\n", pid);
cpid = fork();
if (cpid == 0) {
cpid=getpid();
printf("\n----[%d] child\n", cpid);
if (cpid % 2 == 0) {
flag = 1;
} else {
flag = 0;
}
exit(0);
} else if (cpid != 0) {
wait(NULL);
mypid = getpid();
printf("\n[%d] parent of [%d]\n", mypid, cpid);
if (flag == 1) {
printf("\nThe child pid is even");
} else if(flag ==0) {
printf("\nThe child pid is odd");
} else {
printf("\nSomething went wrong");
}
} else {
perror("Fork failed");
}
return 0;
}
So I have to do the next program:
create a child process,
the parent reads numbers from the keyboard (until 0) and sends them to the child,
the child receives numbers from the parent and prints those that are prime,
when the child receives 0 from the parent, it will terminate.
And I did it the next way:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int a=1;
int isprime=0;
int p[2];
pipe(p);
int pid = fork();
if(pid < 0)
exit(1);
if(pid == 0)
{
close(p[1]);
read(p[0],&a,sizeof(int));
isprime = 1;
for(int i=2;i*i<=a;i++)
if(a%i==0)
isprime = 0;
if(isprime == 1)
printf("%d is prime",a);
close(p[0]);
}
else
{
close(p[0]);
while(a!=0){
printf("a=");
scanf("%d",&a);
write(p[1],&a,sizeof(int));
}
wait(0);
close(p[1]);
}
return 0;
}
But it doesn't work properly, it stops after the first reading of a. Can somebody help me, please?
I am trying to run three execv("./test",execv_str) in parallel. And I need to print out success message when each of execv() completes successfully.
But now I get result as following:
username#username:~/Desktop/$./test -p
SUCCESS
SUCCESS
SUCCESS
username#username:~/Desktop/$ TESTING
TESTING
TESTING
The expected result will be:
username#username:~/Desktop/$./test -p
TESTING
SUCCESS
TESTING
SUCCESS
TESTING
SUCCESS
username#username:~/Desktop/$
Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int fork_execv()
{
int status;
pid_t pid;
pid = fork();
/* Handling Child Process */
if(pid == 0){
char* execv_str[] = {"./test", NULL};
if (execv("./test",execv_str) < 0){
status = -1;
perror("ERROR\n");
}
}
/* Handling Child Process Failure */
else if(pid < 0){
status = -1;
perror("ERROR\n");
}
return status;
}
int main(int argc, char *argv[]){
if (argc == 1){
sleep(5);
printf("TESTING\n");
}
else{
int i;
for(i = 0; i < 3; ++i){
if (fork_execv() != -1){
printf("SUCCESS\n");
}
}
}
}
How to modify my code to make it work?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int fork_execv()
{
int status;
pid_t pid;
pid = fork();
/* Handeling Chile Process */
if(pid == 0){
char* execv_str[] = {"./test", NULL};
if (execv("./test",execv_str) < 0){
status = -1;
perror("ERROR\n");
}
}
/* Handeling Chile Process Failure */
else if(pid < 0){
status = -1;
perror("ERROR\n");
}
return pid;
}
void handler(int sig){
printf("SUCCESS\n");
}
int main(int argc, char *argv[]){
if (argc == 1){
sleep(5);
printf("TESTING\n");
}
else{
int i;
pid_t process_id;
for(i = 0; i < 3; ++i){
if ((process_id = fork_execv()) != -1){
if(process_id != 0){
signal(SIGCHLD, handler);
waitpid(process_id, NULL, 0);
}
}
}
}
}
Here what I would do. After the fork, I return the pid, check if it isn't 0 (so we are in the father process) and make the father wait for the son. To print "success", I bind the SIGCHLD signal that is triggered when a child process ends. Note that this is a little overkill and put print after the waitpid would have done the job. (But I like to bind signal.)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
useconds_t BEAUTY_SLEEP = 500;
int main() {
int remains = 0;
char whoami[50];
pid_t pid;
pid_t ppid;
printf("%d\n", pid); //debug
printf("%d\n", getppid()); //debug
fflush(stdout); //debug
while (1) {
if ((ppid = getppid()) == 2704 && (pid == 0)) {
pid = fork();
if (pid == -1) {
perror("fork()");
exit(1);
}
printf("%ld\n", (long)ppid);
puts("beep");
} else {
usleep(BEAUTY_SLEEP);
}
}
return 0;
}
When I comment out "pid = fork()" in the while loop above, printf("%d\n", pid) prints the proper default value, 0. However, when I restore the pid = fork() line, the print statement outputs a non-zero process ID. Any clue as to why the retroactive assignment is happening?
I want to fork a child process that runs permanently in the background, and parent will prompt the user to enter a line of text. Then display the number of lines entered by the user in child process.
how to do it??
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
int main ( int argc, char *argv[] )
{
pid_t pid;
int mypipe[2];
int ret;
char str[5000];
char buff[2000];
int status = 0;
int lines = 1;
int c = 0;
int line[1000];
ret = pipe(mypipe);
if (ret == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
if (pid == -1)
{
perror("fork");
exit(1);
}
else if (pid == 0)
{
read(mypipe[0],line,50);
printf("Child count line : %d\n", line[0]);
}
else
{
//in parent process
printf("In Parent Process\n");
printf("Enter somthing: \n");
while ((c =getchar())!= '*'){
if (c == '\n'){
lines++;
}
line[0] = lines;
write(mypipe[1],line,sizeof(int));
}
wait(&status);
}
}
The ideal output should look like this:
ds
Child count line : 1
sd
Child count line : 2
sd
Child count line : 3
sd
Child count line : 4