Ask Programming Language Expert

fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as theparent
#include
pid_t fork(void);

The exec() family of functions replaces the current process image with a new process image.

The functions described in this manual page are front-ends for execve(2).

(See the manual page for execve(2) for further details about the replacement of the current process image.)
The exec() family of functions include execl, execlp, execle, execv, execvp, and execvpe to execute a file.

The ANSI prototype for execl() is:

intexecl(const char *path, const char *arg0,..., const char *argn, 0)

#inciude
#inciude


main()
{
execl("/bin/ls", "ls", "-l", 0);
printf("Can only get here on error\n");
}

The first parameter to execl() in this example is the full pathname to the ls command. This is the file whose contents will be run, provided the process has execute permission on the file. The rest of the execl() parameters provide the strings to which the argv array elements in the new program will point. In this example, it means that the ls program will see the string ls pointed to by its argv[0], and the string -l pointed to by itsargv.

In addition to making all these parameters available to the new program, the exec() calls also pass a value for the variable:
extern char **environ;
This variable has the same format as the argv variable except that the items passed via environ are the values in the environment of the process (like any exported shell variables), rather than the command line parameters. In the case of execl(), the value of the environ variable in the new program will be a copy of the value of this variable in the calling process.

The execl() version of exec() is fine in the circumstances where you can ex-plicitly list all of the parameters, as in the previous example. Now suppose you want to write a program that doesn't just run ls, but will run any program you wish, and pass it any number of appropriate command line parameters. Obviously, execl() won't do the job.
The example program below, which implements this requirement, shows, however, that the system call execv() will perform as required:
#inciude

main(intargc, char **argv)
{
if (argc==1)
{
printf("Usage: run []\n");
exit(1)
}
execv(argv[l], &argv[1));
printf("Sorry... couldn't run that!\n");
}
The prototype for execv() shows that it only takes two parameters, the first is the full pathname to the command to execute and the second is the argv value you want to pass into the new program. In the previous example this value was derived from the argv value passed into the runcommand, so that the run command can take the command line parameter values you pass it and just pass them on.
intexecl(pathname, argo, ..., argn, 0);
intexecv(pathname, argv);
intexeclp(cmdname, arg0, ..., argn, 0);
intexecvp(cmdname, argv);
intexecle(patbname, arg0, ..., arga, 0, envp);
intexecve(pathname, argv, envp);

char *pathname, *cmdname;
char *arg0, ..., *argn;
char **argv, **envp;

\pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe
#include
intpipe(intpipefd[2]);

#define _GNU_SOURCE /* See feature_test_macros(7) */#include
/* Obtain O_* constant definitions */#include
int pipe2(intpipefd[2], int flags);

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
#include
ssize_tread(intfd, void *buf, size_t count);

write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
#include
ssize_t write(int fd, const void *buf, size_t count);

close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).
If fd is the last file descriptor referring to the underlying open file description (seeopen(2)), the resources associated with the open file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.
#include
intclose(intfd);

Sample Program 1

/****************************************************************
*
* Example: to demonstrate fork() and execl() and system calls
*
***************************************************************/
#include
#include
#include
#include
#include

int main( intargc, char *argv[], char *env[] )
{
pid_tmy_pid, parent_pid, child_pid;
int status;

/* get and print my pid and my parent's pid. */

my_pid = getpid(); parent_pid = getppid();
printf("\n Parent: my pid is %d\n\n", my_pid);
printf("Parent: my parent's pid is %d\n\n", parent_pid);

/* print error message if fork() fails */
if((child_pid = fork()) < 0 )
{
perror("fork failure");
exit(1);
}

/* fork() == 0 for child process */

if(child_pid == 0)
{ printf("\nChild: I am a new-born process!\n\n");
my_pid = getpid(); parent_pid = getppid();
printf("Child: my pid is: %d\n\n", my_pid);
printf("Child: my parent's pid is: %d\n\n", parent_pid);
printf("Child: I will sleep 3 seconds and then execute - date - command \n\n");

sleep(3);
printf("Child: Now, I woke up and am executing date command \n\n");
execl("/bin/date", "date", 0, 0);
perror("execl() failure!\n\n");

printf("This print is after execl() and should not have been executed if execl were successful! \n\n");

_exit(1);
}
/*
* parent process
*/
else
{
printf("\nParent: I created a child process.\n\n");
printf("Parent: my child's pid is: %d\n\n", child_pid);
system("ps -acefl | grepercal"); printf("\n \n");
wait(&status); /* can use wait(NULL) since exit status
from child is not used. */
printf("\n Parent: my child is dead. I am going to leave.\n \n ");
}

return 0;
}



Sample Program 2

Execution
>gcc-o procproc.c
>gcc-o fibofibo.c
>gcc-o pipe pipe.c
>./proc

proc.c
#include
#include
#include
#include
#include

int main(){

pid_tpid;
intsegment_id; //allocate the memory
char *shared_memory; //pointer to memory
constint size = 4096;
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
shared_memory = (char *) shmat(segment_id, NULL, 0);
pid = fork();
if(pid< 0) { //error
fprintf(stderr, "Fork failed");
return 1;
}
else if(pid == 0){ //child process
char *child_shared_memory;
child_shared_memory = (char *) shmat(segment_id,NULL,0);
//attach mem
sprintf(child_shared_memory, "Hello parent process!");
//write to the shared mem
shmdt(child_shared_memory);

execl("./pipe", "pipe", "3", 0); /* to call pipe program */
execl("./fibo", "fibo", "12", 0); /* to call fibo program */
}
else {//parent process
wait(NULL);
printf("Child process completed.\n");
printf("The child process has written to the shared memory.\n
It has said: %s\n", shared_memory); //read from shared mem
shmdt(shared_memory);
shmctl(segment_id, IPC_RMID, NULL);
}
return 0;
}

fibo.c
#include
#include
#include
#include
#include

intfibo(intnum);

int main(intargc, char *argv[]) {

if (argc != 2) {

fprintf(stderr, "argc %d \n", argc);
fprintf(stderr, "argv[0]: %s >\n", argv[0]);
fprintf(stderr, "argv[1]: %s >\n", argv[1]);
fprintf(stderr, "Usage: %s \n", argv[0]);
exit(EXIT_FAILURE);
}

intnum = atoi(argv[1]);
printf( "%d'th Fibonacci number is %d\n", num, fibo(num));

exit(EXIT_SUCCESS);

} /* end of main */

intfibo(intnum) {

if (num> 0) {
if (num<=2) {
return 1;
} else {
returnfibo(num-1) + fibo(num-2);
}
} else {
return -1;
}

} /* end of fibo() */


pipe.c
#include
#include
#include
#include
#include
#include
#include
#include

intsegmentId;
char *sharedMemory;

int main(intargc, char *argv[]) {

intpipefd[2];
pid_tcpid;
charbuf;

if (argc != 2) {

fprintf(stderr, "argc %d \n", argc);
fprintf(stderr, "argv[0]: %s >\n", argv[0]);
fprintf(stderr, "argv[1]: %s >\n", argv[1]);
fprintf(stderr, "Usage: %s \n", argv[0]);
exit(EXIT_FAILURE);
}

if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}

cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}

if (cpid == 0) { /* Child reads from pipe */
close(pipefd[1]); /* Close unused write end */
while (read(pipefd[0], &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
_exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pipefd[0]); /* Close unsed read end */
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]); /* Reader will see EOF */
wait (NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}

}

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91349596
  • Price:- $50

Guranteed 36 Hours Delivery, In Price:- $50

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As