25 Nisan 2021 Pazar

Threadlere özgü alanlar (pthread_key_create fonksiyonu)

 #include <string.h>

#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct tagPERSON {
    char name[32];
    int no;
} PERSON;
void *thread_proc1(void *param);
void *thread_proc2(void *param);
void foo(void);
pthread_key_t g_slotKey;
int main(void)
{
    pthread_t tid1, tid2;
    int result;
    if (pthread_key_create(&g_slotKey, NULL) != 0) {
        fprintf(stderr, "pthread_create: %s\n", strerror(result));
        exit(EXIT_FAILURE);
    }
    if ((result = pthread_create(&tid1, NULL, thread_proc1, NULL)) != 0) {
        fprintf(stderr, "pthread_create: %s\n", strerror(result));
        exit(EXIT_FAILURE);
    }
    if ((result = pthread_create(&tid2, NULL, thread_proc2, NULL)) != 0) {
        fprintf(stderr, "pthread_create: %s\n", strerror(result));
        exit(EXIT_FAILURE);
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_key_delete(g_slotKey);
    return 0;
}
void *thread_proc1(void *param)
{
    PERSON *per;
    if ((per = (PERSON *)malloc(sizeof(PERSON))) == NULL) {
        fprintf(stderr, "cannot allocate memory!..\n");
        exit(EXIT_FAILURE);
    }
    strcpy(per->name, "Carol David");
    per->no = 333;
    pthread_setspecific(g_slotKey, per);
    foo();
    free(per);
    return NULL;
}
void *thread_proc2(void *param)
{
    PERSON *per;
    if ((per = (PERSON *)malloc(sizeof(PERSON))) == NULL) {
        fprintf(stderr, "cannot allocate memory!..\n");
        exit(EXIT_FAILURE);
    }
    strcpy(per->name, "Ferdinand Isaac");
    per->no = 765;
    pthread_setspecific(g_slotKey, per);
    foo();
    free(per);
    return NULL;
}
void foo(void)
{
    PERSON *per;
    per = (PERSON *)pthread_getspecific(g_slotKey);
    printf("%s, %d\n", per->name, per->no);
}

Share:

23 Nisan 2021 Cuma

Unix Linux sistemlerinde Semapor kullanımı

 

#include <stdio.h>
#include <string.h>
#include<unistd.h>
#include<semaphore.h>
#include<pthread.h>
#include <stdlib.h>
void exit_sys(const char* msg);
void exit_sys_result(const char *msg,int result);

void *thread_proc1(void *param);
void *thread_proc2(void *param);

void do_something(const char *msg);

sem_t g_sem;


int main(void)
{
    printf("Hello World!\n");

    int result;
    pthread_t tid1,tid2;
    srand(time(NULL));


    if(sem_init(&g_sem,0,1)==-1)
    {
        exit_sys("sem_init");
    }

    if((result=pthread_create(&tid1,NULL,thread_proc1,NULL))!=0)
    {
          exit_sys_result("thread create1",result);
    }

    if((result=pthread_create(&tid2,NULL,thread_proc2,NULL))!=0)
    {
           exit_sys_result("thread create2",result);
    }

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    sem_destroy(&g_sem);


    return 0;
}


void *thread_proc1(void *param)
{
    int i;
    for(i=0;i<10;i++)
    {
        do_something(" thread-1 ");
    }
    return NULL;
}

void *thread_proc2(void *param)
{
    int i;
    for(i=0;i<10;i++)
    {
        do_something(" thread-2 ");
    }
    return NULL;
}

void do_something(const char *str)
{
    sem_wait(&g_sem);
    printf("-----------------------\n");
    printf("%s: Step-1\n",str);
    usleep(rand()%300000);
    printf("%s: Step-2\n",str);
    usleep(rand()%300000);
    printf("%s: Step-3\n",str);
    usleep(rand()%300000);
    printf("%s: Step-4\n",str);
    usleep(rand()%300000);
    printf("%s: Step-5\n",str);
    usleep(rand()%300000);
    sem_post(&g_sem);

}
void exit_sys(const char *msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}
void exit_sys_result(const char *msg, int result)
{
    fprintf(stderr, "%s: %s\n", msg, strerror(result));
    exit(EXIT_FAILURE);
}






















Share: