为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

胡汉三66 的学生作业:

sem.h #ifndef __SEM_H__ #define __SEM_H__ #include #include #include #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" #define SEM_SET_PATHNAME "." #define SEM_SET_PRO_ID 106 union semun{ unsigned short *array;//指针指向内存,保存信息量集合的值 }; //创建信号量集合 int sem_create(int nsems,unsigned short values[]) { key_t key; int semid,ret; union semun s; //返回key值 key = ftok(SEM_SET_PATHNAME,SEM_SET_PRO_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);//数组指针array 与 命令SETALL 配合使用 if (ret == -1) { perror("[ERROR] semctl() :"); return -1; } return semid; } //占用资源 int sem_p(int semid,int semnum) { struct sembuf sops; sops.sem_num = semnum; sops.sem_op = -1; sops.sem_flg = SEM_UNDO;//进程终止时,会自动释放信号量 semop(semid,&sops,1);//占用信号量 } //释放资源 int sem_v(int semid,int semnum) { struct sembuf sops; sops.sem_num = semnum; sops.sem_op = 1; sops.sem_flg = SEM_UNDO;//进程终止时,会自动释放信号量 semop(semid,&sops,1);//释放信号量 } //删除信息量集合 int sem_del(int semid) { return semctl(semid,0,IPC_RMID,NULL); } main.c #include "sem.h" #define SEM_CONTROL_P 0 //控制父进程的信号量编号 #define SEM_CONTROL_C 1 //控制子进程的信号量编号 int main(void) { pid_t cpid; int semid; unsigned short values[2] = {1,0};//信号量的值 time_t t; struct tm *ptm = NULL;//要设置空指针,不然会报错 //创建信号量集合,集合有两个信号量,信号量的值分别为1,0 semid = sem_create(2,values); if (semid == -1) return -1; cpid = fork(); if (cpid == -1) { perror("[ERROR] fork() :"); exit(EXIT_FAILURE); }else if(cpid == 0){ for(int j = 1;j tm_year + 1900,//从1900年开始计算 ptm->tm_mon + 1,//月份从0开始计算,'0+1'表示第一月 ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec); fflush(stdout); sem_v(semid,SEM_CONTROL_P);//P=0===>P=1,释放资源 } }else if(cpid > 0){ for(int i = 1;i P=0,占用资源 printf("%d\n"); sem_v(semid,SEM_CONTROL_P);//P=0===>P=1,释放资源 } wait(NULL); sem_del(semid);//删除信号量集合 } return 0; } Makefie 1 all: 2 gcc sem.c main.c -o main 【图片】

得分 100
学习任务

大禹123 的学生作业:

main.c #include #include #include #include #include #include #include #include #define PATHNAME "./" #define PROJID 33 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SIZE 128 typedef struct msgbuf { long mtype; char mtext[MSG_SIZE]; } msgbuf_t; int get_message_id(char *pathname, int projid) { key_t key; int msgid; key = ftok(pathname, projid); if (key == -1) { perror("[ERROR] ftok():"); return -1; } msgid = msgget(key, IPC_CREAT | 0666); if (msgid == -1) { perror("[ERROR] msgget():"); return -1; } return msgid; } int main() { int msgid; msgbuf_t msg; char buffer[MSG_SIZE]; char msgstr[16]; // 足够大以存储 msgid 转换为字符串后的结果 pid_t cpid_A, cpid_B; msgid = get_message_id(PATHNAME, PROJID); if (msgid == -1) { fprintf(stderr, "create message fail!\n"); exit(EXIT_FAILURE); } // 将 msgid 转换为字符串 sprintf(msgstr, "%d", msgid); // 创建子进程 A cpid_A = fork(); if (cpid_A == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if (cpid_A == 0) { // 子进程 A 执行 process_a execl("./process_a", "./process_a", msgstr, NULL); perror("[ERROR] execl(): "); exit(EXIT_FAILURE); } else { // 创建子进程 B cpid_B = fork(); if (cpid_B == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if (cpid_B == 0) { // 子进程 B 执行 process_b execl("./process_b", "./process_b", msgstr, NULL); perror("[ERROR] execl(): "); exit(EXIT_FAILURE); } else { // 父进程:处理用户输入并发送消息 char command[MSG_SIZE] = {0}; char name; while (1) { memset(command, 0, sizeof(command)); memset(&msg, 0, sizeof(msg)); sleep(1); printf("please select send msg to A or B:\n"); scanf("%c", &name); getchar(); if (name == 'A') { msg.mtype = MSG_TYPE_A; } else if (name == 'B') { msg.mtype = MSG_TYPE_B; } else { printf("select error!\n"); continue; } printf("please input msg content to %c, or input quit to exit:\n", name); fgets(command, sizeof(command), stdin); // 移除 fgets 读取的换行符 command[strcspn(command, "\n")] = 0; if (strcmp(command, "quit") == 0) { printf("GoodBye\n"); break; } strcpy(msg.mtext, command); int ret = msgsnd(msgid, (const void *)&msg, strlen(msg.mtext) + 1, 0); if (ret == -1) { perror("[ERROR] msgsnd():"); continue; } } // 发送信号给子进程以通知它们结束 kill(cpid_A, SIGUSR1); kill(cpid_B, SIGUSR2); // 等待子进程结束 wait(NULL); wait(NULL); exit(EXIT_SUCCESS); } } return 0; } process_a.c #include #include #include #include #include #include #include #include #define PATHNAME "./" #define PROJID 33 #define MSG_TYPE_A 100 #define MSG_TYPE_B 200 #define MSG_SIZE 128 typedef struct msgbuf { long mtype; char mtext[MSG_SIZE]; } msgbuf_t; int get_message_id(char *pathname, int projid) { key_t key; int msgid; key = ftok(pathname, projid); if (key == -1) { perror("[ERROR] ftok():"); return -1; } msgid = msgget(key, IPC_CREAT | 0666); if (msgid == -1) { perror("[ERROR] msgget():"); return -1; } return msgid; } int main() { int msgid; msgbuf_t msg; char buffer[MSG_SIZE]; char msgstr[16]; // 足够大以存储 msgid 转换为字符串后的结果 pid_t cpid_A, cpid_B; msgid = get_message_id(PATHNAME, PROJID); if (msgid == -1) { fprintf(stderr, "create message fail!\n"); exit(EXIT_FAILURE); } // 将 msgid 转换为字符串 sprintf(msgstr, "%d", msgid); // 创建子进程 A cpid_A = fork(); if (cpid_A == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if (cpid_A == 0) { // 子进程 A 执行 process_a execl("./process_a", "./process_a", msgstr, NULL); perror("[ERROR] execl(): "); exit(EXIT_FAILURE); } else { // 创建子进程 B cpid_B = fork(); if (cpid_B == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if (cpid_B == 0) { // 子进程 B 执行 process_b execl("./process_b", "./process_b", msgstr, NULL); perror("[ERROR] execl(): "); exit(EXIT_FAILURE); } else { // 父进程:处理用户输入并发送消息 char command[MSG_SIZE] = {0}; char name; while (1) { memset(command, 0, sizeof(command)); memset(&msg, 0, sizeof(msg)); sleep(1); printf("please select send msg to A or B:\n"); scanf("%c", &name); getchar(); if (name == 'A') { msg.mtype = MSG_TYPE_A; } else if (name == 'B') { msg.mtype = MSG_TYPE_B; } else { printf("select error!\n"); continue; } printf("please input msg content to %c, or input quit to exit:\n", name); fgets(command, sizeof(command), stdin); // 移除 fgets 读取的换行符 command[strcspn(command, "\n")] = 0; if (strcmp(command, "quit") == 0) { printf("GoodBye\n"); break; } strcpy(msg.mtext, command); int ret = msgsnd(msgid, (const void *)&msg, strlen(msg.mtext) + 1, 0); if (ret == -1) { perror("[ERROR] msgsnd():"); continue; } } // 发送信号给子进程以通知它们结束 kill(cpid_A, SIGUSR1); kill(cpid_B, SIGUSR2); // 等待子进程结束 wait(NULL); wait(NULL); exit(EXIT_SUCCESS); } } return 0; } process_b.c // 从运行的时候参数中获取消息id #include #include #include #include #include #include #define MSG_TYPE 200 #define MSG_SIZE 128 typedef struct msgbuf { long mtype; char mtext[MSG_SIZE]; }msgbuf_t; void init_mesasge(msgbuf_t *msg, int msg_type) { memset(msg, 0, sizeof(*msg)); msg->mtype = msg_type; } int main(int argc, char *argv[]) { if(argc!=2) { fprintf(stdout,"Usage: %s ", argv[0]); exit(EXIT_FAILURE); } int msgid; msgbuf_t msg; ssize_t rbytes; msgid = atoi(argv[1]); init_mesasge(&msg, MSG_TYPE); while(1) { rbytes = msgrcv(msgid, (void *)&msg, sizeof(msg.mtext), MSG_TYPE, 0); if(rbytes == -1) { perror("[ERROR] msgrcv():"); exit(EXIT_FAILURE); } if(rbytes>0) { printf("process B recives message: %s\n", msg.mtext); } } return 0; } 1、gcc process_a.c -o process_a 2、gcc process_b.c -o process_b 3、gcc main.c -o main 4、./main

得分 100
学习任务

wgf1209 的学生作业:

Judge.hpp #ifndef __JUDGE_HPP__ #define __JUDGE_HPP__ #include #include #include #include "Player.hpp" #include "ChessBoard.hpp" /* @Author: wjhu8 @Time: 2024-11-15 16:14:09 @name: Judge.hpp */ //标准的命名空间 using namespace std; //裁判类 class Judge { public: bool isWin(Player *player) { bool ok = false; string chessColor = player->getColor(); ok = isHorziontalWin(chessColor);//水平方向的判断 if (ok) { return true; } ok = isVerticalWin(chessColor);//锤子方向的判断 if (ok) { return true; } ok = isUphillWin(chessColor);//上坡方向的判断 if (ok) { return true; } ok = isDownhillWin(chessColor);//下坡方向的判断 if (ok) { return true; } return ok; } //水平方向的判断 bool isHorziontalWin(const string &chessColor) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); //right 向右判断 for (int i = 0; i < 5; i++) { if (chessBoard->isSameColorChess(chessColor, curLine, curColumn + i)) { count++; } else { break; } } // if (count >= 5) { return true; } //left 向左判断 for (int i = 0; i < 5; i++) { if (chessBoard->isSameColorChess(chessColor, curLine, curColumn - i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } //锤子方向的判断 bool isVerticalWin(const string &chessColor) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); //up 向上判断 for (int i = 0; i < 5; i++) { //行减少,列不变 if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn)) { count++; } else { break; } } // if (count >= 5) { return true; } //down 向下判断 for (int i = 0; i < 5; i++) { //行增加,列不变 if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } //上坡方向的判断 bool isUphillWin(const string &chessColor) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); //uphill--->up 斜坡向上判断 for (int i = 0; i < 5; i++) { //行减少,列增多 if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn + i)) { count++; } else { break; } } // if (count >= 5) { return true; } //uphill---> down 斜坡向下判断 for (int i = 0; i < 5; i++) { //行增加,列减少 if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn - i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } //下坡方向的判断 bool isDownhillWin(const string &chessColor) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); //Downhill--->up 下坡向上判断 for (int i = 0; i < 5; i++) { //行减少,列增多 if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn - i)) { count++; } else { break; } } // if (count >= 5) { return true; } //Downhill---> down 下坡向下判断 for (int i = 0; i < 5; i++) { //行增加,列减少 if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn + i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } private: }; #endif //__JUDGE_HPP__ main.cpp #include #include #include #include #include #include //标准算法头文件 #include "head/BlackPlayer.hpp" #include "head/WhitePlayer.hpp" #include "head/KeyHandle.hpp" #include "head/Judge.hpp" /* @Author: wjhu8 @Time: 2024-11-14 16:39:47 @name: main.cpp */ //标准的命名空间 using namespace std; int main() { //创建棋盘对象 ChessBoard *chessBoard = ChessBoard::getChessBoard(); chessBoard->show();//显示棋盘 BlackPlayer blackPlayer("铁蛋"); //黑色棋手 WhitePlayer whitePlayer("翠花"); //白色棋手 Player *players[] = {&blackPlayer, &whitePlayer}; #if 0 blackPlayer.placeChess(5, 1); whitePlayer.placeChess(5, 3); #endif //创建键盘对象 KeyHandle keyHandle; //创建裁判对象 Judge judge; #if 1 while (1) { for(Player *player:players){ keyHandle.waitPlaceChess(player); //调用光标 bool ok = judge.isWin(&blackPlayer); if (ok) { break; } } } #endif return 0; } 【图片】

得分 100
学习任务

大禹123 的学生作业:

#include #include #include #include #include #include // 总体逻辑顺序 /* 1、先让父进程睡眠,让子进程AB分别执行一段 2、让子进程暂停,等待信号 3、父进程唤醒,让父进程发送杀死子进程型号 4、子进程收到立刻终止信号,不执行后面的语句 5、父进程依次收到子进程结束的语句 6、父进程结束 */ int main() { pid_t cpid_A, cpid_B; cpid_A = fork(); if(cpid_A == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); }else if(cpid_A == 0) { //子进程A fprintf(stdout, "child process A start\n",getpid()); pause(); fprintf(stdout, "child process A end\n",getpid()); exit(EXIT_SUCCESS); }else if(cpid_A > 0) { //父进程 cpid_B = fork(); if(cpid_B == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); }else if(cpid_B == 0) { //子进程B fprintf(stdout, "child process B start\n",getpid()); pause(); fprintf(stdout, "child process B end\n",getpid()); exit(EXIT_SUCCESS); }else if(cpid_B > 0) { //父进程 sleep(1); fprintf(stdout, "father process start\n",getpid()); kill(cpid_A, SIGKILL); kill(cpid_B, SIGKILL); //父进程 int status_A = 0, status_B = 0; pid_t rcpid_A = 0, rcpid_B = 0; while((rcpid_A = waitpid(cpid_A, &status_A, WNOHANG))==0); printf("rcpid_A:%d exit\n", rcpid_A); while((rcpid_B = waitpid(cpid_B, &status_B, WNOHANG))==0); printf("rcpid_B:%d exit\n",rcpid_B); fprintf(stdout, "father process end\n",getpid()); exit(EXIT_SUCCESS); } } return 0; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号