Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
-
- void main ()
- {
- while (1) {
- char buf[20];
- int returnStatus;
- printf("Podaj polecenie do wykonania [d,s,c,t]:\n");
- fgets(buf, 20, stdin);
- pid_t pid = fork();
- if (pid==0) {
- printf("Tu potomek pid=%d\n", getpid());
- switch(buf[0]){
- case 'd':
- execlp("date", "date", NULL);
- exit(0);
- break;
- case 's':
- execlp("sh","sh", NULL);
- exit(0);
- break;
- case 'c':
- execlp("xclock","xclock", NULL);
- exit(0);
- break;
- case 't':
- execlp("xterm","xterm", NULL);
- exit(0);
- break;
- default:
- printf("Nie istnieje takie polecenie\n");
- exit(0); /* obowiazkowe zakonczenie potomka */
- break;
- }
- }
- if (buf[0] == 'd' || buf[0] == 's'){
- wait(NULL);
- }
- if (buf[0] == 'c' || buf[0] == 't'){
- waitpid(pid, &returnStatus, WNOHANG);
- }
- printf("Tu rodzic po utworzeniu potomka.\n");
- } /* nieskonczona petla rodzica */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement