From c23d7e1ccccad031a736c2274821cfb7edd4db09 Mon Sep 17 00:00:00 2001 From: flykhan Date: Fri, 18 Aug 2023 15:03:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=98=9F=E5=88=97=E6=A1=88?= =?UTF-8?q?=E4=BE=8B:=20=E5=8D=95=E6=9C=BA=E8=81=8A=E5=A4=A9=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day5/chat_tool/bob | Bin 0 -> 9392 bytes day5/chat_tool/chat_tool.c | 88 +++++++++++++++++++++++++++++++++++++ day5/chat_tool/lucy | Bin 0 -> 9392 bytes 3 files changed, 88 insertions(+) create mode 100755 day5/chat_tool/bob create mode 100644 day5/chat_tool/chat_tool.c create mode 100755 day5/chat_tool/lucy diff --git a/day5/chat_tool/bob b/day5/chat_tool/bob new file mode 100755 index 0000000000000000000000000000000000000000..9c3ad5403b36fd10a44f42f2f65fdc7ab4227d51 GIT binary patch literal 9392 zcmcIqdu&_P89%m@#(g>NgA!V_-gN|(j=D)mLsGQX&Z{>iZL>55#z=1*KN3^Nj_m6+ z=^qg0rmB^NWg4*V52Q#N3??QrX|RroCT&Mk#6Lw7LlaEu9%VL#tq4|S59)p2x#zR* zz44vcA3K%rcfQ~EJkNX2Ngr?PY`5ENjEbG@X2{Jty&Te>j9tH-vjS^m%bAyLX1B9t zfRw|}%Oyy)8qr1Jny6FgIe?bIuhFgnjdm}u5T$TLDMO?fuUNdIs?k+0jU~#M!~wL! zT-`$McJ*w{mLrN0`>9=(RvMLM9J>!V262r(c%`0-6EbOIN0r;fK9{iE*Q1BZDe>PHDU%y~Wha1NmSr4OHeW4cm@Pq}v(E{(Xz;{{T4CYY)r%eM(s)@kx7?N0EXqX-IM^rW(N+jY5rYMnQ zJkZpnBm@3fm<^}YBn!tAM?sKO{eh!OVDP9C_D7;@C^?V_j4&lMiq?rpObw&QXefrF z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O^Sco?&X@N1wi?ibCkh}!&nXgi)U-HKV$a-hv#rk z*d@_mJfCBg?5i{}djKeG`2~AA|BQ=7-w=94%>8!+1bB9)_;gNc7YW}f3@LxoBYG6% zcorv~&Qrs}z$@&{`3sog_b;YO_H^!=753Clif?ZJ31QzX9MXNlC1F7K56Ge~v;C8R zVV_`rSm+$rhm!&d-K%JvmMhhfX5go{r}N&pu$N37PJjC#`jp9@&V@4~QOXx%VKMX1 z0;Z2o-$k=R@jOlrU7@%H{SYd8dpc*T;zEk3mzYS~L92WN(iDCdggzqi&+}3jO`wtZ(Mu0ozKQ zJ)M`U&?ywNzM206=wSa-d^(p(U(o&ou*JDc_H_QH|6f7>kAmhm#ix02+7zGu9=eE) zf`&rKVihS0p^4?OY3=Ckvm*alv5$NSggf9z V?V)`U*Jr8tr-j1(CZRe0e*iejbX@=d literal 0 HcmV?d00001 diff --git a/day5/chat_tool/chat_tool.c b/day5/chat_tool/chat_tool.c new file mode 100644 index 0000000..695b94a --- /dev/null +++ b/day5/chat_tool/chat_tool.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct msg_ +{ + long mType; // 消息类型 + char content[100]; // 正文数据 + char name[32]; // 姓名 +} MSG; + +int main(int argc, char const *argv[]) +{ + if (argc != 2) + { + printf("invalid, format is : ./xx name\n"); + return 1; + } + + key_t key = ftok("/", 160); + int msgqid = msgget(key, IPC_CREAT | 0666); + if (msgqid == -1) + { + perror("msgget"); + return 1; + } + int pid = fork(); + if (pid == 0) + { + // 子进程 + while (1) + { + write(STDOUT_FILENO, "我说:", 7); + MSG msg; + scanf("%s", msg.content); + strcpy(msg.name, argv[1]); +#ifdef BOB + msg.mType = 1; +#else + msg.mType = 2; +#endif + if (msgsnd(msgqid, &msg, sizeof(MSG) - sizeof(long), 0) != -1) + { + printf("send msg OK\n"); + if (strncmp(msg.content, "bye", 3) == 0) + { + break; + } + } + } + // kill(getppid(), SIGSTOP); + _exit(0); + } + else if (pid > 0) + { + // 父进程 + while (1) + { + MSG msg; +#ifdef BOB + msg.mType = 2; +#else + msg.mType = 1; +#endif + if (msgrcv(msgqid, &msg, sizeof(MSG) - sizeof(long), msg.mType, 0) != -1) + { + printf("\n%s >>> \"%s\"\n", msg.name, msg.content); + + if (strncmp(msg.content, "bye", 3) == 0) + { + break; + } + write(STDOUT_FILENO, "我说:", 7); + } + } + // kill(pid, SIGSTOP); + wait(NULL); + } + + return 0; +} diff --git a/day5/chat_tool/lucy b/day5/chat_tool/lucy new file mode 100755 index 0000000000000000000000000000000000000000..aab34505431e878622803f62a3d08300fde01bfb GIT binary patch literal 9392 zcmcIqdu&_P89%m@)_pl?DJ8UMz3T`H9dVP8hO}s{omX#4(q?H2j1g`eKN3?vBKx{c z`Ue=&RJB4_rUB~yK#H`%VA@2G2J4t;(q=S8{8NM&nqX4*D6@HNMX)M+Q1AQBJ)eE= zjqk+%*r|NK^ZmZ(dE9f)Iq9SAT^)9tjZv|)Jq)>7r-wt@ld-F}a8_W=Y!&mct?U-I z0+4d}dAJ0rRwKG3ToZK)JqOS-_%+)#pxN%>6`~Z5C}oHg;}wfXR5iNRrLjaAlQ@8O zm}{kwSGoo`PIN}(@q(aahb-FF2)i0#NA!|Nj3~7a+lIeB;jho`0SEmBUV_RSBxPNG7lk2UQ z-%;ZsC)&~vs$aPr0nH61v=J+X-)DiZvA}m);L{d(zyjZEf%jYBk67TxEbwOlm*8hE zTLCE554Tz12@8C)1>R$U@3z1h%p*Ten zziXG0^!s8VHj+}4EEG>10YOsr`Hv|6;Uh}O7ml!Kawy?H%9P+JS|`FWHH01`!5E5$ zf+`EC@grDI#sXlD;n7DzRieIdj2%ma)ga(Q(RfVY3ftG!+0v>sxbG^|8Vj`sH%v3Mw`*%|~ybj-p=LEKq^A7fs;8UB!tI*4@fXA{_F~BLl>tc!E zd{w1}uLKVLQ;~FdVQB+ljSk26kV>5nr?mxT%{pAa{(5w{e*KYkIOas9M~Bl|iL(23 zI5fCW26Xu90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