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和mutex?我们被要求用一个带有id的结构方法(每个应该有6个线程,每个睡眠时间不同。),sec(sleeptime)和int信号[6]。 这是一项学校任务,我们没有接受任何c-training培训。请帮忙。
答案 0 :(得分:0)
您可以通过相同的互斥锁同步访问不同线程之间的任何共享数据,然后在主线程中执行join_all
以等待所有线程完成。< / p>
我将实现留给读者。
答案 1 :(得分: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 =) 谢谢你的帮助!