# Include <pthread. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <semaphore. h> # include <unistd. h> # define thread_num 25 typedef struct {file * _ fp; int _ nthreadid; // sem_t * _ semlock;} idd_thread_param; void * threadfunc (void * ARGs) {char Sline [100 + 1]; file * fpread = (idd_thread_param *) ARGs)-> _ fp; sem_t * semlock = (idd_thread_param *) ARGs) -> _ semlock; int nid = (idd_thread_param *) RGS)-> _ nthreadid; sem_wait (semlock); While (! Feof (fpread) {memset (Sline, 0, sizeof (Sline); fgets (Sline, 100, fpread); fprintf (stderr, "thread ID-% d: % s ", NID, Sline) ;}sem_post (semlock) ;}int main () {pthread_t * pthreads; sem_t semlock; pthreads = (pthread_t *) malloc (thread_num * sizeof (pthread_t); sem_init (& semlock, 0, 1); file * fp = fopen ("test.txt", "R "); // start thread loop idd_thread_param Param; For (INT I = 0; I <thread_num; I ++) {memset (Param, 0, sizeof (idd_thread_param); Param. _ fp = FP; Param. _ nthreadid = I; Param. _ semlock = & semlock; pthread_create (pthreads + I), null, threadfunc, Param) ;}for (INT I = 0; I <thread_num; I ++) pthread_join (* (pthreads + I), null); free (pthreads); pthreads = NULL; fclose (FP); FP = NULL; return 0 ;}