Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //zad1
- #include <stdio.h>
- #include <process.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <pthread.h>
- #include <unistd.h>
- int main(int argc, char *argv[])
- {
- int pid1, pid2, stat1, stat2, i, p;
- int pid[100];
- pid[1]=getpid();
- printf("Program stworzyl proces macierzysty (nr: 1) o PID: %d\n", pid[1]);
- for(i=2; i<argc && pid1!=0; i++)
- {
- pid1 = fork();
- if(pid1 == 0)
- {
- pid[i]=getpid();
- printf("Utworzylem proces nr: %d o PID: %d PPID: %d\n", i, pid[i], getppid());
- }
- }
- sleep(1);
- if(pid1 != 0)printf("\n");
- sleep(1);
- for(i=1; i<argc; i++)
- {
- // printf("TEST %d argc: %d\n", i, argc);
- for(p=1; p<= atoi(argv[i]) && pid[i]==getpid() ; p++)
- {
- //sleep(1);
- printf("PID = %d wykonay = %d PPID: %d\n", getpid(), p, getppid());
- sleep(1);
- if(atoi(argv[i])==p)
- {
- if(pid1 == 0)
- {
- printf("Zamykam proces PID = %d\n", getpid());
- }
- else
- {
- printf("Zamkniento proces macierzysty! o PID = %d\n", getpid());
- }
- exit(EXIT_SUCCESS);
- }
- }
- }
- return 0;
- }
- /*
- exit(25);
- pid = wait(&status);
- int w = WEXITSTATUS(status);
- printf("Status zakonczenia procesu(%d): %d\n", pid, w);
- */
- //wywolanie funkcji
- ./one 7 5 3 1
- //zad2
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void* function( void* arg )
- {
- while(1) {
- printf( "This is thread %d\n", pthread_self() );
- sleep(1);
- }
- pthread_exit( 0 );
- }
- int main( void )
- {
- getchar();
- pthread_create( NULL, NULL, &function, NULL );
- // while(1) {
- printf( "This is thread %d\n", pthread_self() );
- sleep(1);
- // }
- sleep( 5 );
- getchar();
- return EXIT_SUCCESS;
- }
- //zad3
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void* function( void* arg )
- {
- printf( "This is thread %d\n", pthread_self() );
- sleep(1);
- sleep(40);
- pthread_exit(0);
- }
- int main( void )
- {
- pthread_t moj_watek;
- // getchar();
- pthread_create( &moj_watek, NULL, &function, NULL );
- printf( "This is thread %d\n", pthread_self() );
- sleep(1);
- // sleep(40);
- pthread_join(moj_watek,NULL);
- // sleep( 5 );
- return EXIT_SUCCESS;
- }
- //zad4
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void* function( void* arg )
- {
- int liczba;
- printf( "This is thread %d %d\n", pthread_self(), *((int*)arg) );
- liczba = *((int*)arg);
- liczba*=liczba;
- // sleep(3);
- pthread_exit((void*)liczba);
- }
- int main( void )
- {
- int liczba;
- int wynik;
- pthread_t moj_watek;
- void* wynik_raw;
- scanf("%d",&liczba);
- pthread_create( &moj_watek, NULL, &function, (void*)(&liczba) );
- printf( "This is thread %d\n", pthread_self() );
- // sleep(2);
- pthread_join(moj_watek,&wynik_raw);
- wynik = (int)wynik_raw;
- printf("%d\n",wynik);
- return EXIT_SUCCESS;
- }
- //lab1
- Kod:
- #include <stdio.h>
- #include <sys/types.h>
- #include <process.h>
- int main( )
- {
- int potomny, i, nr_m, nr_p;
- potomny = fork();
- if(potomny==0) //potomny
- {
- for(i=0; i<10; i++)
- {
- nr_m=getpid();
- nr_p=getppid();
- printf("ID procesu macierzystego: %d \n", nr_m);
- printf("ID procesu potomnego: %d \n", nr_p);
- sleep(1);
- }
- }
- else //macierzysty
- {
- nr_m=getpid();
- printf("ID procesu macierzystego: %d \n", nr_m);
- getchar();
- }
- }
- Zad 2
- Kod:
- #include <stdio.h>
- #include <sys/types.h>
- #include <process.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main( )
- {
- int potomny, i, nr_m, nr_p, status, id;
- potomny = fork();
- if(potomny==0) //potomny
- {
- for(i=0; i<10; i++)
- {
- nr_m=getpid();
- nr_p=getppid();
- printf("ID procesu macierzystego: %d \n", nr_m);
- printf("ID procesu potomnego: %d \n", nr_p);
- sleep(1);
- }
- exit(1);
- }
- else //macierzysty
- {
- nr_m=getpid();
- printf("Czekam...\n");
- //sleep(10); //przypadek 2
- id = wait(&status);
- printf("ID procesu: %d \n", nr_m);
- }
- }
- Zad 3
- Kod:
- #include <stdio.h>
- #include <sys/types.h>
- #include <process.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main( )
- {
- int potomny, i, nr_m, nr_p, status, id;
- potomny = fork();
- if(potomny==0) //potomny
- {
- for(i=0; i<5; i++)
- {
- nr_m=getpid();
- nr_p=getppid();
- printf("ID procesu macierzystego: %d \n", nr_m);
- printf("ID procesu potomnego: %d \n", nr_p);
- sleep(1);
- }
- exit(25);
- }
- else //macierzysty
- {
- nr_m=getpid();
- printf("Czekam...\n");
- id = wait(&status);
- if(WIFEXITED(status)>0)
- {
- printf("Kod powrotu: %d \n", WEXITSTATUS(status));
- }
- else if(WIFSIGNALED(status)>0)
- {
- printf("Nr sygnalu: %d \n",WTERMSIG(status));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement