fork:
pid_t fork (void).
pid_t <sys/types.h> int. . fork .
, Unix , . fork . fork , , .
, .
exit . :
void exit (int exit_code),
exit_code .
:
#include <iostream.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{ int status;
if (fork())
{ cout << "Child process\n";
exit(0);
};
cout << "Parent process\n";
}
wait waitpid . :
pid_t wait (int status_p);
pid_t waitpid (pid_t child_pid, int* status_p,
int options).
wait , . , wait status_p, PID .
waitpid , child_pid , .
, (, .) , exec. , execl:
int execl (const char* path, const char* arg, ).
, , . arg , exec. argv main . arg argv[0], , arg, - argv[1] . . .
.
#include <stdio.h>
#include <unistd.h>
int main()
{
char s1[8]="string1", s2[4]="string2";
execl ("./b.out", s1, s2, 0);
return 0;
}
b.out,
|
|
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("%s\n",argv[0]);
printf("%s\n",argv[1]);
return 0;
}
string1
string2
exec , exec . exec fork, fork exec.
pipe . , , . , . :
int pipe (int fifo[2]).
fifo , , pipe. Unix , . . fifo[0], fifo[1].
, , .
#include <iostream.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int child_pid;
int fifo[2], status;
int sf=sizeof(float);
float buf1=2;
pipe(fifo);
write(fifo[1],&buf1,sf);
child_pid=fork();
if (child_pid==0)
{ float buf2;
read(fifo[0],&buf2,sf);
buf2=buf2+1;
write(fifo[1],&buf2,sf);
exit(0);
};
wait(&status);
read(fifo[0],&buf1,sf);
cout << "buf1=" << buf1;
}
.