0
void process(int number, int time) {
    printf("Prosess %d kjører\n", number);
    sleep(time);
    printf(" Prosess %d terminated after %d sekunder\n", number, time);
}

int main(void)  {
    pid_t pid[7];
    int status= 0;

    if((pid[1]= fork())== 0)    {
        process(1, 1);
        exit(0);
    }
    if((pid[3]= fork())== 0)    {
        process(3, 3);
        exit(0);
    }
    waitpid(pid[1], NULL, 0);

    if((pid[5]= fork())== 0)    {       
        process(5, 3);
        exit(0);
    }       
    if((pid[2]= fork())== 0)    {
        process(2, 2);
        exit(0);
    }
    waitpid(pid[3], NULL, 0);
    //waitpid(pid[2], NULL, 0);
    if((pid[4]= fork())== 0)    {
        process(4, 2);
        exit(0);
    }
    waitpid(pid[5], NULL, 0);

    if((pid[6]= fork())== 0)    {   
        process(6, 3);
        exit(0);
    }   
    wait(NULL);
        while(wait(&status)> 0) {
        //We just wait for the children to finish their processes
    }
    printf("All processes is now terminated\n");
    return 0;
}

我怎样才能把这段代码做同样的事情,而是使用 pthread 和互斥锁?我们被要求创建一个带有 id 的结构方法(应该有 6 个线程,每个线程睡眠不同的时间。),sec(睡眠时间)和 int 信号 [6]。它是一项学校任务,我们没有接受任何 c 培训。请帮忙。

4

2 回答 2

0

您将通过同一个互斥锁同步对不同线程之间的任何共享数据的访问,然后您将join_all在主线程中执行一个等待所有线程完成的操作。

我将实施留给读者。

于 2013-02-14T10:44:40.927 回答
0
sem_t sem[6];     /* one semaphore for each thread */

struct threadargs {
  int id;         /* thread number */
  int sec;        /* how many seconds to sleep */
  int signal[6];  /* which threads to signal when done */
};


void* tfunc(void *arg) {
  int i;
  struct threadargs *targs=arg;
// VENT PÅ DIN EGEN SEMAFOR
  sem_wait(&sem[targs->id-1]);
  printf("Tråd %d kjører\n", targs->id);
  sleep(targs->sec);
  printf("Tråd %d er ferdig og vekker kanskje andre...\n", targs->id);
// ITERATE OVER Signal-array and wake up thread nr "i" if Signal[i] is 1
  for(i= 0; i< 6; i++)  {
    if(targs->signal[i]== 1)
        sem_post(&sem[i]);
  }
}


int main(void)
{
  int i,j;
  pthread_t tid[6];
  struct threadargs *targs[6];

  /* allocate memory for threadargs and zero out semaphore signals */
  for(i=0;i<6;i++) { 
    targs[i] = (struct threadargs*) malloc(sizeof(struct threadargs));
    for(j=0;j<6;j++) 
    targs[i]->signal[j]=0;
  }

  targs[0]->id=1;             /* thread number 1 */
  targs[0]->sec=1;            /* how long to sleep */
  targs[0]->signal[1]=1;      /* which threads to wake up when done */
  targs[0]->signal[4]=1;
// INITIALIZES THREADS SEMAPHORE TO 1 OR 0
  sem_init(&sem[0], SHARED, 1);
// START THREAD
  pthread_create(&tid[0], NULL, tfunc, (void*) targs[0]);

  targs[1]->id= 2;             /* thread number 2 */
  targs[1]->sec=2;            /* how long to sleep */
  targs[1]->signal[3]=1;      /* which threads to wake up when done */
  sem_init(&sem[1], SHARED, 0);
  pthread_create(&tid[1], NULL, tfunc, (void*) targs[1]);

  targs[2]->id= 3;             /* thread number 3 */
  targs[2]->sec=3;            /* how long to sleep */
  sem_init(&sem[2], SHARED, 0);
  pthread_create(&tid[2], NULL, tfunc, (void*) targs[2]);

  targs[3]->id= 4;             /* thread number 4 */
  targs[3]->sec=2;            /* how long to sleep */
  sem_init(&sem[3], SHARED, 0);
  pthread_create(&tid[3], NULL, tfunc, (void*) targs[3]);

  targs[4]->id= 5;             /* thread number 5 */
  targs[4]->sec=3;            /* how long to sleep */
  targs[4]->signal[5]=1;      /* which threads to wake up when done */
  sem_init(&sem[4], SHARED, 0);
  pthread_create(&tid[4], NULL, tfunc, (void*) targs[4]);

  targs[5]->id= 6;             /* thread number 6 */
  targs[5]->sec=3;            /* how long to sleep */
  sem_init(&sem[5], SHARED, 0);
  pthread_create(&tid[5], NULL, tfunc, (void*) targs[5]);

  for(i=0;i<6;i++) 
    pthread_join(tid[i], NULL);

  return 0;
}

这是我使用的代码。带信号量的 Pthreads =) 谢谢你的帮助!

于 2013-02-17T13:39:59.897 回答