fork.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { p
ID: 3740989 • Letter: F
Question
fork.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0) { /* an error occurred */
perror("fork failed");
exit(-1);
} else if(pid == 0) { /* child process */
printf("I AM THE CHILD PROCESS... sleeping for 30 seconds ");
sleep(30);
printf("CHILD FINISHES ");
} else { /* parent process */
printf("I AM THE PARENT PROCESS... sleeping for 60 seconds ");
sleep(60);
printf("PARENT FINISHES ");
}
return 0;
}
myls.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0) { /* an error occurred */
perror("fork failed");
exit(-1);
} else if(pid == 0) { /* child process */
printf("I AM A CHILD PROCESS ");
execlp("/bin/ls", "ls", "-l", NULL);
/* return only when exec fails */
perror("exec failed");
exit(-1);
} else { /* parent process */
wait(0);
printf("PARENT PROCESS FOUND CHILD PROCESS COMPLETED ");
}
return 0;
}
myshell.c
/* This example is stolen from Dr. Raju Rangaswami's original 4338
demo and modified to fit into our lecture. */
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX_ARGS 20
#define BUFSIZ 1024
int get_args(char* cmdline, char* args[])
{
int i = 0;
/* if no args */
if((args[0] = strtok(cmdline, " ")) == NULL)
return 0;
while((args[++i] = strtok(NULL, " ")) != NULL) {
if(i >= MAX_ARGS) {
printf("Too many arguments! ");
exit(1);
}
}
/* the last one is always NULL */
return i;
}
void execute(char* cmdline)
{
int pid, async;
char* args[MAX_ARGS];
int nargs = get_args(cmdline, args);
if(nargs <= 0) return;
if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) {
exit(0);
}
/* check if async call */
if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; }
else async = 0;
pid = fork();
if(pid == 0) { /* child process */
execvp(args[0], args);
/* return only when exec fails */
perror("exec failed");
exit(-1);
} else if(pid > 0) { /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call ");
} else { /* error occurred */
perror("fork failed");
exit(1);
}
}
int main (int argc, char* argv [])
{
char cmdline[BUFSIZ];
for(;;) {
printf("COP4338$ ");
if(fgets(cmdline, BUFSIZ, stdin) == NULL) {
perror("fgets failed");
exit(1);
}
execute(cmdline) ;
}
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include<stdlib.h>
#include<errno.h>
void pipeOperation()
{
int i,j,pfd[2];
i=fork();
if(i==0)
{
pipe(pfd);
j=fork();
if(j==0)
{
close(1);
dup(pfd[1]);
close(pfd[0]);
execl("/bin/ls","ls",NULL);
}
else
{
close(0);
dup(pfd[0]);
close(pfd[1]);
close(pfd[0]);
execl("/bin/sort","sort",NULL);
}
}
else
{
wait();//original parent
}
}
int main()
{
pipeOperation();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.