.


:




:

































 

 

 

 


. 2

. 1.

:

, . , : , , .

1. :

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int i;

for(i = 0; i <3; i++)

{

pid_t pid = fork();

printf("I am process %d, My parent is %d\n", getpid(), getppid());

}

7 .

  1 2 11
i=0 1 - - -
i=1 2 11 - -
i=2 3 12 21 111

 

 
 

 

 


2.

% (echo ' 1'; echo ' 2') > plan.txt

:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main(){

FILE *fp;

char str[80];

fp = fopen("plan.txt", "r");

if(fp == NULL)

{

fprintf(stderr, "Could not open file");

exit(1);

}

fgets(str, 80, fp);

printf("Parent reads: %s\n", str);

if(fork() == 0)

{

fgets(str, 80, fp);

printf("Child Reads: %s\n", str);

}

fclose(fp);

}

, ?

, ..

fork,

.

 

, .

:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main(void)

{

FILE *fp;

fp = fopen("plan.txt","w");

if(fp == NULL)

{

fprintf(stderr,"Could not open file");

exit(1);

}

fprintf(fp," \n");

if(!fork())

{

fprintf(fp," \n");

}

fclose(fp);

}

2. , , . .

       
   
 

 


:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main(void)

{

int pid_1;

int pid_11;

int pid_12;

int pid_2;

int pid_21;

int pid_22;

int status;

pid_1=fork();

if (pid_1== 0)

{

pid_11=fork();

if(pid_11==0)

printf("I am the second generation child process - %d. My parent is - %d\n", getpid(), getppid()); //

else

{

wait(&status);

pid_12=fork();

if(pid_12==0)

printf("I am the second generation child process - %d. My parent is - %d\n",getpid(),getppid());//

else

{

wait(&status);

printf("I am the first generation child process - %d. My parent is - %d\n",getpid(),getppid()); //

}

}

}

else

{

wait(&status);

pid_2=fork();

if(pid_2==0)

{

pid_21=fork();

if(pid_21==0)

printf("I am the second generation child process - %d. My parent is - %d\n",getpid(),getppid());//

 

else

{

wait(&status);

pid_22=fork();

if(pid_22==0)

printf("I am the second generation child process - %d. My parent is - %d\n",getpid(),getppid()); //

 

else

{

wait(&status);

printf("I am the first generation child process - %d. My parent is - %d\n",getpid(),getppid()); //

}

}

}

else

{

wait(&status);

printf("I am the parent process - %d.\n",getpid()); //

}

}

}

:

I am the second generation child process - 4083. My parent is - 4082

I am the second generation child process - 4084. My parent is - 4082

I am the first generation child process - 4082. My parent is - 4081

I am the second generation child process - 4086. My parent is - 4085

I am the second generation child process - 4087. My parent is - 4085

I am the first generation child process - 4085. My parent is - 4081

I am the parent process - 4081.

 

. 2.

:

. , : , , .

 

.

1. , . :

.

fork

, exec .

exec exit(1)

 

:

exc21_1.

#include <stdio.h>

#include <unistd.h>

int main(void)

{

int pid=fork();

if(pid==0)

{

execv("exc21_2.ou",NULL);

printf ("EXEC Failed\n");

_exit(1);

}

else

 

printf("Parent id=%d and child id=%d\n",getpid(),pid);

}

exc21_2.

#include <stdio.h>

int main(void)

{

printf("Child id=%d\n",getpid());

}

 

:

Parent id=4566 and child id=4567

Child id=4567

 

2. , 2 - . . exit

 

exc22_1.

#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int main(void)

{

int status;

if(!fork())

{

execv("exc22_2.ou", NULL);

printf ("EXEC Failed\n");

_exit(1);

}

else

{

wait(&status);

printf("First child end\n");

if(!fork())

{

execv("exc22_3.ou", NULL);

printf ("EXEC Failed\n");

_exit(1);

}

else

{

wait(&status);

printf("Second child end\n");

}

}

}

 

exc22_2.

#include <stdio.h>

int main(void)

{

printf("Hello! I am first child\n");

}

exc22_3.

#include <stdio.h>

int main(void)

{

printf("Hello! I am second child\n");

}

 

 

:

 

Hello! I am first child

First child end

Hello! I am second child

Second child end

 

 

3. , .

#include <sys/types.h>

#include <sys/wait.h>

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

int main(int argc, char * argv[])

{

int pid, status;

if (argc < 2)

{

printf("Usage: %s command, [arg1 [arg2]...]\n", argv[0]);

return EXIT_FAILURE;

}

printf("Starting %s...\n", argv[1]);

pid = fork();

if (pid == 0)

{

execvp(argv[1], &argv[1]);

perror("execvp");

return EXIT_FAILURE; // Never get there normally

}

 

else

{

if (wait(&status) == -1)

{

perror("wait");

return EXIT_FAILURE;

}

if (WIFEXITED(status))

printf("Child terminated normally with exit code %i\n", WEXITSTATUS(status));

if (WIFSIGNALED(status))

printf("Child was terminated by a signal #%i\n", WTERMSIG(status));

if (WCOREDUMP(status))

printf("Child dumped core\n");

if (WIFSTOPPED(status))

printf("Child was stopped by a signal #%i\n", WSTOPSIG(status));

}

return EXIT_SUCCESS;

}

 

:

user:~$./exec.ou ps

Starting ps...

PID TTY TIME CMD

2020 pts/1 00:00:00 bash

4919 pts/1 00:00:00 exec.ou

4920 pts/1 00:00:00 ps

Child terminated normally with exit code 0

 

 

: , , .



<== | ==>
|
:


: 2016-09-03; !; : 791 |


:

:

- - , .
==> ...

1623 - | 1588 -


© 2015-2024 lektsii.org - -

: 0.041 .