Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- labs.cs.upt.ro/~septimiu.dinulica/s7.html
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- int main (int argc, char* argv[]) {
- int pid;
- int status;
- int N = atoi(argv[1]);
- int i;
- if (argc < 3) {
- printf("wrong arguments\n");
- exit(-1);
- }
- for(i=0; i<N; i++) {
- if ((pid = fork()) < 0) {
- printf("error forking\n");
- exit(-2);
- }
- if (pid == 0) {
- printf("Eu sunt %d si am parinte pe %d. Am fost pornit al %d-lea.\n", getpid(), getppid(), i+1);
- exit(i);
- }
- if (wait(&status) != -1) {
- printf("Parinte, terminat proces fiu %d cu valoarea de exit %d.\n", pid, WEXITSTATUS(status));
- }
- }
- for(i=2; i < argc; i++) {
- if ((pid=fork()) < 0) {
- printf("error forking 2\n");
- exit(-3);
- } else if (pid == 0) {
- printf("Numar cuvinte fisier %s:\n", argv[i]);
- execlp("wc", "wc", "-w", argv[i], NULL);
- }
- wait(&status);
- }
- exit(5);
- }
- labs.cs.upt.ro/~septimiu.dinulica/s8.html
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <signal.h>
- int pid_copil;
- int status;
- char litera;
- void rutinaAlarma(int a) {
- alarm(2);
- if(kill(pid_copil, SIGUSR1)!=0) {
- printf("error SIGUSR1\n");
- }
- }
- void rutinaUSR1(int a) {
- if(litera > 'c') {
- exit(1);
- } else {
- printf("%c \n", litera);
- litera++;
- }
- }
- void rutinaSC (int a) {
- pid_copil = wait(&status);
- printf("child with pid %d ended with code %d\n", pid_copil, WEXITSTATUS(status));
- exit(0);
- }
- int main (int argc, char* argv[]) {
- struct sigaction sigActAlarm;
- sigActAlarm.sa_handler = rutinaAlarma;
- sigActAlarm.sa_flags = 0;
- struct sigaction sigActSC;
- sigActSC.sa_handler = rutinaSC;
- sigActSC.sa_flags = 0;
- struct sigaction sigActUSR1;
- sigActUSR1.sa_handler = rutinaUSR1;
- sigActUSR1.sa_flags = 0;
- if ((pid_copil = fork()) < 0) {
- printf("error forking\n");
- exit(-1);
- } else if (pid_copil == 0) {
- sigaction(SIGUSR1, &sigActUSR1, NULL);
- litera = 'a';
- while(1);
- } else {
- sigaction(SIGALRM, &sigActAlarm, NULL);
- sigaction(SIGCHLD, &sigActSC, NULL);
- /*
- if (raise (SIGALRM) == 0) {
- printf("succ raised alarm\n");
- } else {
- printf("err raising alarm\n");
- }
- */
- alarm(2);
- while(1);
- }
- }
- labs.cs.upt.ro/~septimiu.dinulica/s9.html
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
- #include <fcntl.h>
- int pid_c1;
- int pid_c2;
- int pid_c;
- int status;
- char litera;
- int main (int argc, char* argv[]) {
- int stat_cifre[10] = {0,0,0,0,0,0,0,0,0,0};
- int pfd1[2], pfd2[2], pfd3[2];
- int fd = open(argv[1], O_RDONLY);
- char buff[1000];
- int buff_size;
- int i;
- if (pipe(pfd1) < 0) {
- printf("error pipe 1\n");
- exit(-1);
- }
- if (pipe(pfd2) < 0) {
- printf("error pipe 2\n");
- exit(-2);
- }
- if (pipe(pfd3) < 0) {
- printf("error pipe 3\n");
- exit(-3);
- }
- if((pid_c1 = fork()) < 0) {
- printf("error forking 1\n");
- exit(-4);
- } else if (pid_c1 == 0) {
- close(pfd1[1]); // 0 = citire, 1 = scriere
- close(pfd2[0]);
- close(pfd3[0]);
- close(pfd3[1]);
- char buff_str[1];
- while(buff_size = read(pfd1[0], buff, 1000)) {
- for(i=0; i<buff_size; i++) {
- if(buff[i] >= '0' && buff[i] <= '9') {
- //printf("char: %c\n", buff[i]);
- //sprintf(buff_str, "%c", buff[i]);
- buff_str[0] = buff[i];
- //printf("str: %s\n", buff_str);
- write(pfd2[1], buff_str, 1);
- }
- }
- }
- close(pfd2[1]);
- exit(1);
- } else if ((pid_c2 = fork()) < 0) {
- printf("error forking 2\n");
- exit(-5);
- } else if (pid_c2 == 0) {
- close(pfd1[0]);
- close(pfd1[1]);
- close(pfd2[1]);
- close(pfd3[0]);
- char c[10];
- int cifre_dist = 0;
- char cifre_str[100];
- while(read(pfd2[0], c, 1)) {
- //printf("%s\n", c);
- stat_cifre[atoi(c)]++;
- }
- FILE* out = fopen("output.txt", "w");
- for(i=0; i<10; i++) {
- sprintf(buff, "%d: %d\n", i, stat_cifre[i]);
- fprintf(out, "%s",buff);
- if(stat_cifre[i] != 0)
- cifre_dist++;
- }
- fclose(out);
- sprintf(cifre_str, "%d", cifre_dist);
- write(pfd3[1], cifre_str, 100);
- close(pfd3[1]);
- exit(2);
- } else {
- close(pfd1[0]);
- close(pfd2[0]);
- close(pfd2[1]);
- close(pfd3[1]);
- while(buff_size = read(fd, buff, 1000)) {
- write(pfd1[1], buff, buff_size);
- }
- close(pfd1[1]);
- while(read(pfd3[0], buff, 1000)) {
- printf("cifre distincte: %s\n", buff);
- }
- close(pfd3[0]);
- for(i=0; i<2; i++) {
- pid_c = wait(&status);
- printf("child %d ended with %d\n", pid_c, WEXITSTATUS(status));
- }
- }
- }
- labs.cs.upt.ro/~septimiu.dinulica/s10.html
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
- #include <fcntl.h>
- int pid_c1;
- int pid_c2;
- int pid_c;
- int status;
- char litera;
- int main (int argc, char* argv[]) {
- int pfd1[2], pfd2[2], pfd3[2];
- FILE *stream;
- int fd;
- int numar_cuvinte = 0;
- if (pipe(pfd1) < 0) {
- printf("error pipe 1\n");
- exit(-1);
- }
- if (pipe(pfd2) < 0) {
- printf("error pipe 2\n");
- exit(-2);
- }
- if (pipe(pfd3) < 0) {
- printf("error pipe 3\n");
- exit(-3);
- }
- if((pid_c1 = fork()) < 0) {
- printf("error forking 1\n");
- exit(-4);
- } else if (pid_c1 == 0) {
- //close(pfd1[1]); // 0 = citire, 1 = scriere
- close(pfd2[0]);
- close(pfd3[0]);
- close(pfd3[1]);
- dup2(pfd2[1],1);
- execlp("ps", "ps", "ax", NULL);
- printf("\n");
- close(pfd2[1]);
- exit(1);
- } else if ((pid_c2 = fork()) < 0) {
- printf("error forking 2\n");
- exit(-5);
- } else if (pid_c2 == 0) {
- //close(pfd1[0]);
- //close(pfd1[1]);
- close(pfd2[1]);
- close(pfd3[0]);
- dup2(pfd2[0], 0);
- dup2(pfd3[1], 1);
- execlp("wc", "wc", "-w", NULL);
- close(pfd3[1]);
- close(pfd2[0]);
- exit(2);
- } else {
- //close(pfd1[0]);
- close(pfd2[0]);
- close(pfd2[1]);
- //close(pfd3[1]);
- stream = fdopen(pfd3[0], "r");
- fscanf(stream, "%d", &numar_cuvinte);
- printf("numar cuvinte numarate: %d\n", numar_cuvinte);
- close(pfd3[1]);
- close(pfd3[0]);
- for(int i=0; i<2; i++) {
- pid_c = wait(&status);
- printf("child %d ended with status %d\n", pid_c, WEXITSTATUS(status));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement