Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 2_6
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <string.h>
- pthread_t t1, t2, t3, t4;
- pthread_mutex_t mutex;
- int number=0;
- void* produce(void* arg);
- void* display(void* arg);
- void zad2_6() {
- int res=0;
- pthread_mutex_init(&mutex, NULL);
- res = pthread_create(&t1, NULL, produce, (void*)1);
- if (res!=0){
- printf("Thread creation failed!\n");
- }
- pthread_create(&t2, NULL, produce, (void*)2);
- if (res!=0){
- printf("Thread creation failed!\n");
- }
- pthread_create(&t3, NULL, produce, (void*)3);
- if (res!=0){
- printf("Thread creation failed!\n");
- }
- pthread_create(&t4, NULL, display, NULL);
- if (res!=0){
- printf("Thread creation failed!\n");
- }
- pthread_join(t4, NULL);
- }
- void *produce(void *arg) {
- while (1) {
- pthread_mutex_lock(&mutex);
- number = (int*)arg;
- pthread_mutex_unlock(&mutex);
- sleep(1);
- }
- }
- void* display(void* arg) {
- while (1) {
- pthread_mutex_lock(&mutex);
- printf("%d\n", number);
- pthread_mutex_unlock(&mutex);
- sleep(3);
- }
- }
- /*
- 2_9
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <mqueue.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- mqd_t queue;
- pthread_t t1, t2, t3;
- struct message {
- char mtext[128];
- int id;
- };
- void* sender_thread(void* arg) {
- int th_id = (int*)arg;
- struct message buf;
- struct message msg = {
- .mtext = "Wiadomosc",
- .id = th_id
- };
- while (1) {
- mq_send(queue, (const char*)& msg, sizeof(buf), 1);
- sleep(th_id);
- }
- }
- void* receiver_thread(void* arg) {
- struct message buf;
- while (1) {
- int n;
- n = mq_receive(queue, (char*) &buf, sizeof(buf), NULL);
- if (n != -1) {
- printf("Otrzymano wiadomość o treści: %s od wątku %d \n", buf.mtext, buf.id);
- }
- }
- }
- void zad2_9() {
- struct mq_attr mattr = {
- .mq_maxmsg = 10,
- .mq_msgsize = sizeof(struct message)
- };
- queue = mq_open("/myq",
- O_CREAT | O_RDWR,
- S_IREAD | S_IWRITE,
- &mattr);
- pthread_create(&t1, NULL, sender_thread, (void*)1);
- pthread_create(&t2, NULL, sender_thread, (void*)2);
- pthread_create(&t3, NULL, receiver_thread, NULL);
- pthread_join(t1, NULL);
- }
- /*
- 2_10
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <string.h>
- pthread_barrier_t bariera;
- pthread_t t1, t2, t3;
- void* calculate(void* arg){
- int id = (int*)arg;
- printf("Watek %d wykonuje obliczenia...\n", id);
- int waittime = rand()%10 + 1;
- sleep(waittime);
- printf("Watek %d skonczyl obliczenia, oczekuje na pozostale watki\n", id);
- pthread_barrier_wait(&bariera);
- printf("Synchronizacja udana\n");
- pthread_exit(0);
- }
- void zad2_10() {
- pthread_barrier_init(&bariera, NULL, 3);
- pthread_create(&t1, NULL, calculate, (void*)1);
- pthread_create(&t2, NULL, calculate, (void*)2);
- pthread_create(&t3, NULL, calculate, (void*)3);
- pthread_join(t1, NULL);
- pthread_join(t2, NULL);
- pthread_join(t3, NULL);
- }
- /*
- 3_5
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <string.h>
- #define RD 5
- #define WR 2
- pthread_rwlock_t rwlock;
- pthread_t readers[RD];
- pthread_t writers[WR];
- int dane[3] = {0,0,0};
- void* reader_fun(void* arg){
- int id = (int*)arg;
- while(1){
- usleep(500000*id);
- pthread_rwlock_rdlock(&rwlock);
- printf("Czytelnik %d: %d %d %d\n", id, dane[0], dane[1], dane[2]);
- usleep(100000);
- pthread_rwlock_unlock(&rwlock);
- }
- }
- void* writer_fun(void* arg){
- int id = (int*)arg;
- while(1){
- usleep(5000000*id);
- pthread_rwlock_wrlock(&rwlock);
- printf("Pisarz %d zapisuje dane...\n", id);
- usleep(300000);
- dane[0] = id;
- dane[1] = id+1;
- dane[2] = id+2;
- pthread_rwlock_unlock(&rwlock);
- }
- }
- void zad3_5(){
- int i, res;
- pthread_rwlock_init(&rwlock, NULL);
- for (i=0; i<RD; i++){
- res = pthread_create(&(readers[i]), NULL, reader_fun, (void*)(i+1));
- if(res!=0){
- printf("Error!\n");
- }
- }
- for (i=0; i<WR; i++){
- res = pthread_create(&(writers[i]), NULL, writer_fun, (void*)(i+1));
- if(res!=0){
- printf("Error!\n");
- }
- }
- pthread_join(readers[0], NULL);
- }
- /*
- 3_6
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <string.h>
- #define NO_OF_PHIL 5
- pthread_mutex_t kelner;
- pthread_mutex_t widelec[NO_OF_PHIL];
- pthread_t filozof[NO_OF_PHIL];
- void* philo_fun(void* arg){
- int id = (int*)arg;
- int thinkTime;
- int fork1 = id;
- int fork2 = id+1;
- if(fork2 == NO_OF_PHIL){
- fork2 = 0;
- }
- while(1){
- //myślenie
- printf("Filozof %d myśli...\n", id+1);
- thinkTime = rand()%8 + 1;
- sleep(thinkTime);
- //staje sie glodny
- printf("Filozof %d stal sie glodny\n", id+1);
- //podniesienie widelcow
- pthread_mutex_lock(&kelner);
- pthread_mutex_lock(&(widelec[fork1]));
- pthread_mutex_lock(&(widelec[fork2]));
- pthread_mutex_unlock(&kelner);
- //jedzenie
- printf("Filozof %d je\n", id+1);
- sleep(3);
- //oddawanie widelcow
- pthread_mutex_unlock(&(widelec[fork1]));
- pthread_mutex_unlock(&(widelec[fork2]));
- }
- }
- void zad3_6(){
- int i;
- pthread_mutex_init(&kelner, NULL);
- for(i=0; i<NO_OF_PHIL; i++){
- pthread_mutex_init(&(widelec[i]), NULL);
- }
- for(i=0; i<NO_OF_PHIL; i++){
- pthread_create(&(filozof[i]), NULL, philo_fun, (void*)i);
- }
- pthread_join(filozof[0], NULL);
- }
- /*
- 3_10
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <string.h>
- #define NO_OF_CONS 5
- pthread_mutex_t cond_mutex;
- pthread_cond_t cond;
- pthread_cond_t finished;
- pthread_t consumers[5];
- pthread_t producer;
- int data = 0;
- int count = 0;
- void* producer_fun(void* arg){
- while(1){
- while(count != 0){
- pthread_mutex_lock(&cond_mutex);
- pthread_cond_wait(&finished, &cond_mutex);
- pthread_mutex_unlock(&cond_mutex);
- }
- printf("Producent modyfikuje dane...\n");
- sleep(5);
- data = rand()%100;
- count = 5;
- pthread_cond_broadcast(&cond);
- }
- }
- void* consumer_fun(void* arg){
- int id = (int*)arg;
- while(1){
- pthread_mutex_lock(&cond_mutex);
- pthread_cond_wait(&cond, &cond_mutex);
- pthread_mutex_unlock(&cond_mutex);
- printf("Konsument %d odczytal informacje %d\n", id, data);
- pthread_mutex_lock(&cond_mutex);
- count--;
- if(count==0){
- pthread_cond_signal(&finished);
- }
- pthread_mutex_unlock(&cond_mutex);
- }
- }
- void zad3_10(){
- int i=0;
- pthread_mutex_init(&cond_mutex, NULL);
- pthread_cond_init(&cond, NULL);
- pthread_cond_init(&finished, NULL);
- pthread_create(&producer, NULL, producer_fun, NULL);
- for(i=0; i<NO_OF_CONS; i++){
- pthread_create(&(consumers[i]), NULL, consumer_fun, (void*)i);
- }
- pthread_join(producer, NULL);
- }
- /*
- 4.9
- */
- #include <stdio.h>
- #include <omp.h>
- #define N 10
- void zad4_9(){
- float A[N], B[N/2], max;
- int i, th_id;
- for(i=0; i<N; i++){
- A[i] = (float) (rand()%100 + 1);
- printf("%f ", A[i]);
- }
- printf("\n");
- max = A[0];
- omp_set_num_threads(N/2);
- #pragma omp parallel default(none), private(i, max, th_id), shared(A, B)
- {
- th_id = omp_get_thread_num();
- max = A[th_id*2];
- #pragma omp for
- for(i=0; i<N; i++){
- if(A[i] > max)
- max = A[i];
- }
- B[th_id] = max;
- }
- for(i=0; i<N/2; i++){
- printf("%f ", B[i]);
- if(B[i] > max)
- max = B[i];
- }
- printf("\n");
- printf("Max: %f\n", max);
- // printf("Max: %d\n", max);
- }
- /*
- 4.10
- */
- #include <stdio.h>
- #include <omp.h>
- #include <math.h>
- #define R 100000
- #define SHOTS 10000000
- float odleglosc(int x1, int y1, int x2, int y2){
- return sqrt( (float)(x2-x1)*(x2-x1) + (float)(y2-y1)*(y2-y1) );
- }
- void zad4_10(){
- int inside=0, total=0, x, y, i;
- float distance, pi;
- omp_set_num_threads(10);
- #pragma omp parallel default(none), private(x, y, distance, i), reduction(+:inside,total)
- {
- #pragma omp for
- for(i=0; i<SHOTS; i++){
- x = rand()%(R*2 + 1) - R;
- y = rand()%(R*2 + 1) - R;
- distance = odleglosc(x, y, 0, 0);
- if(distance <= R)
- inside++;
- total++;
- }
- }
- pi = (float)4*((float)inside/total);
- printf("Inside: %d\nTotal: %d\nPI: %f\n", inside, total, pi);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement