On the Linux system, write and run a C program that will: fork into two processe
ID: 3885886 • Letter: O
Question
On the Linux system, write and run a C program that will:
fork into two processes: the parent will wait for the child to finish and print ‘We are all done.’ The child will execute the ‘ls’ command to print out a list of the files in the directory.
You may use any editor – ed, vi, pico, emacs – to create your program (save the file as filename.c, where filename is any name you choose). To compile use the c compiler (gcc) on your program:
gcc filename.c
The result of the compilation, assuming no errors, is a file a.out. Since your directory is not in the default path, you run the program as:
./a.out
That’s a period, followed by a slash, followed by a.out.
Submit: the name of your source file (filename.c) and the path to the directory it is in, which should be your account name (that is you default directory) and maybe a subdirectory, if you created one. I will read (maybe compile) and run your program.
Explanation / Answer
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(){
int p = fork();
if(p < 0){
printf("fork failed");
return 0;
}
if(p == 0){
System(ls);
}else{
wait(5);
printf(" We are all done. ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.