
作业社区
探索学习新天地,共享知识资源!
大禹123 的学生作业:
sem.h #ifndef __SEM_H__ #define __SEM_H__ #include #include #include #include #include #include extern int sem_create(int nsems, unsigned short values[]); //创建信号量的集合 //占用资源 extern int sem_p(int semid, int semnum); //释放资源 extern int sem_v(int semid, int semnum); //删除信号量集合 extern int sem_del(int semid); #endif sem.c #include "sem.h" /* 创建信号量的集合 @param nsems : 信号量的个数 @param values : 信号量的值的数组 */ #define SEM_PATH_NAME "." #define PROJ_ID 125 typedef union semun { unsigned short *array; //SETALL }semun_u; int sem_create(int nsems, unsigned short values[]) { key_t key; int semid, ret; semun_u s; key = ftok(SEM_PATH_NAME, PROJ_ID); if(key == -1) { perror("[ERROR] ftok():"); return -1; } semid = semget(key, nsems, IPC_CREAT|0644); if(semid == -1) { perror("[ERROR] semget():"); return -1; } s.array = values; ret = semctl(semid, 0, SETALL, s); if(ret == -1) { perror("[ERROR] semctl():"); return -1; } return semid; } /*占用资源 @param semid:信号量集合id @param semnum:信号量编号 */ int sem_p(int semid, int semnum) { struct sembuf sops; sops.sem_num = semnum; //信号量编号 sops.sem_op = -1; //-1 表示占用资源 sops.sem_flg = SEM_UNDO; //进程终止,自动释放信号量 return semop(semid, &sops, 1); } //释放资源 int sem_v(int semid, int semnum) { struct sembuf sops; sops.sem_num = semnum; //信号量编号 sops.sem_op = 1; //1 表示释放资源 sops.sem_flg = SEM_UNDO; //进程终止,自动释放信号量 return semop(semid, &sops, 1); } //删除信号量 int sem_del(int semid) { return semctl(semid,0, IPC_RMID, NULL); } main.c #include #include #include #include #include #include #include "sem.h" #define SEM_CONTROL_C 0 #define SEM_CONTROL_P 1 int main(void) { pid_t cpid; int semid; unsigned short values[2] = {0, 1}; //表示信号量只有两个 int index = 1; semid = sem_create(2, values); if(semid == -1) { exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1){ perror("[ERROR] fork(): "); exit(EXIT_FAILURE); }else if (cpid == 0){ time_t t; struct tm *lc = NULL; while(1){ sem_p(semid, SEM_CONTROL_C); time(&t); lc = localtime(&t); int year = lc->tm_year + 1900; int month = lc->tm_mon + 1; int day = lc->tm_mday; int hours = lc->tm_hour; int min = lc->tm_min; int sec = lc->tm_sec; printf("%d-%d-%d %d:%d:%d",year,month,day,hours,min,sec); fflush(stdout); sem_v(semid, SEM_CONTROL_P); } }else if (cpid > 0){ while(1){ sem_p(semid, SEM_CONTROL_P); printf("%d,\n"); fflush(stdout); sem_v(semid, SEM_CONTROL_P); sleep(1); } wait(NULL); sem_del(semid); } } Makefile main: sem.c main.c gcc $^ -o main 结果【图片】





胡汉三66 的学生作业:
#include #include #include #include //创建两个子线程,并打印两个子线程的tid void *do_pthread_1(void *arg) { printf("pthread one start.\n"); pthread_exit(NULL); } void *do_pthread_2(void *arg) { printf("pthread two start.\n"); pthread_exit(NULL); } int main(void) { int err_1,err_2; pthread_t tid_1 = 0,tid_2 = 0; err_1 = pthread_create(&tid_1,NULL,do_pthread_1,NULL); if (err_1 != 0) { fprintf(stderr,"[ERROR] pthread_create one() : %s\n",strerror(err_1)); } err_2 = pthread_create(&tid_2,NULL,do_pthread_2,NULL); if (err_2 != 0) { fprintf(stderr,"[ERROR] pthread_create two() : %s\n",strerror(err_2)); } printf("tid_1 = %ld\n",tid_1); printf("tid_2 = %ld\n",tid_2); //pthread_join(tid_1,NULL);//等待子线程tid_1退出,并释放子线程tid_1资源 //pthread_join(tid_2,NULL);//等待子线程tid_2退出,并释放子线程tid_2资源 pthread_detach(tid_1);//分离线程,不阻塞主线程,在线程退出后,由操作系统自动释放线程的资源 pthread_detach(tid_2);// #if 0 while(1) { // } #endif return 0; } 【图片】





胡汉三66 的学生作业:
#include #include #include #include //创建两个子线程,并打印两个子线程的tid void *do_pthread_1(void *arg) { printf("pthread one start."); } void *do_pthread_2(void *arg) { printf("pthread two start."); } int main(void) { int err_1,err_2; pthread_t tid_1 = 0,tid_2 = 0; err_1 = pthread_create(&tid_1,NULL,do_pthread_1,NULL); if (err_1 != 0) { fprintf(stderr,"[ERROR] pthread_create one() : %s\n",strerror(err_1)); } err_2 = pthread_create(&tid_2,NULL,do_pthread_2,NULL); if (err_2 != 0) { fprintf(stderr,"[ERROR] pthread_create two() : %s\n",strerror(err_2)); } printf("tid_1 = %ld\n",tid_1); printf("tid_2 = %ld\n",tid_2); return 0; } 【图片】




