进程的双向通信,消息队列的方式
#include <gsj.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int main()
{
key_t key = ftok("./", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n", key);
int msgid = msgget(key, IPC_CREAT | 0664);
if (msgid < 0)
{
perror("msgget");
return -1;
}
printf("msgid=%d\n", msgid);
//--------------------------------------------------
struct msgbuf snd;
//-------------------------------------------------
pid_t pid = fork();
while (1)
{
if (pid > 0)
{
while (1)
{
sleep(0);
ssize_t msgs = msgrcv(msgid, &snd, sizeof(snd.mtext), 2, 0);
if (msgs < 0)
{
perror("msgsnd");
return -1;
}
printf("msgs=%ld mtype=%ld 小郭说=%s\n", msgs, snd.mtype, snd.mtext);
}
}
else if (pid == 0)
{
snd.mtype = 1;
printf("消息类型为-->%ld\n", snd.mtype);
// scanf("%ld", &snd.mtype);
// getchar();
printf("请输入消息内容-->\n");
fgets(snd.mtext, sizeof(snd.mtext), stdin);
snd.mtext[strlen(snd.mtext) - 1] = '\0';
//-------------------------------------------------
// while (1)
// {
int msgf = msgsnd(msgid, &snd, sizeof(snd.mtext), 0);
if (msgf < 0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
else
{
perror("fork");
return -1;
}
}
// ssize_t msgs = msgrcv(msgid, &snd, sizeof(snd.mtext), 0, 0);
// if (msgs < 0)
// {
// perror("msgsnd");
// return -1;
// }
// printf("msgs=%s\n", snd.mtext);
// system可以让c代码执行终端命令
system("ipcs -q");
return 0;
}
#include <gsj.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int main()
{
key_t key = ftok("./", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n", key);
int msgid = msgget(key, IPC_CREAT | 0664);
if (msgid < 0)
{
perror("msgget");
return -1;
}
printf("msgid=%d\n", msgid);
//--------------------------------------------------
struct msgbuf snd;
//--------------------------------------------------
pid_t pid = fork();
while (1)
{
if (pid > 0)
{
while (1)
{
sleep(0);
ssize_t msgs = msgrcv(msgid, &snd, sizeof(snd.mtext), 1, 0);
if (msgs < 0)
{
perror("msgsnd");
return -1;
}
printf("msgs=%ld mtype=%ld 小吴说=%s\n", msgs, snd.mtype, snd.mtext);
}
}
else if (pid == 0)
{
snd.mtype = 2;
printf("消息类型为-->%ld\n", snd.mtype);
// scanf("%ld", &snd.mtype);
// getchar();
printf("请输入消息内容-->\n");
fgets(snd.mtext, sizeof(snd.mtext), stdin);
snd.mtext[strlen(snd.mtext) - 1] = '\0';
//-------------------------------------------------
// while (1)
// {
int msgf = msgsnd(msgid, &snd, sizeof(snd.mtext), 0);
if (msgf < 0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
else
{
perror("fork");
return -1;
}
}
// system可以让c代码执行终端命令
system("ipcs -q");
return 0;
}
内存共享 信号灯集循环倒转打印1234567
#include <gsj.h>
int main()
{
key_t key = ftok("./", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
// printf("key=%#x\n", key);
//-----------------------------------------------------
int shmid = shmget(key, 128, IPC_CREAT | 0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
// printf("shmid=%d\n", shmid);
//------------------------------------------------------
void *shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *)-1)
{
perror("shmat");
return -1;
}
// printf("shmaddr=%p\n", shmaddr);
//------------------------------------------------------
int semid = semget(key, 2, IPC_CREAT | 0664);
if (semid < 0)
{
perror("semget");
return -1;
}
//------------------------------------------------------
struct sembuf pos;
char *ptr = (char *)shmaddr;
strcpy(ptr, "1234567");
char temp;
int j = 0;
while (1)
{
pos.sem_num = 1;
pos.sem_op = -1;
pos.sem_flg = 0;
int sem = semop(semid, &pos, 1);
if (sem < 0)
{
perror("semop");
return -1;
}
for (int i = 0, j = strlen(ptr) - 1; i < j; i++, j--)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
return 0;
}
#include <gsj.h>
int main()
{
key_t key = ftok("./", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
// printf("key=%#x\n", key);
//-----------------------------------------------------
int shmid = shmget(key, 128, IPC_CREAT | 0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
// printf("shmid=%d\n", shmid);
//------------------------------------------------------
void *shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *)-1)
{
perror("shmat");
return -1;
}
// printf("shmaddr=%p\n", shmaddr);
//-------------------------------------------------------
int semid = semget(key, 2, IPC_CREAT | 0664);
if (semid < 0)
{
perror("semget");
return -1;
}
//------------------------------------------------------
struct sembuf pos;
char *ptr = (char *)shmaddr;
strcpy(ptr, "1234567");
char temp;
int j = 0;
while (1)
{
pos.sem_num = 1;
pos.sem_op = +1;
pos.sem_flg = 0;
int sem = semop(semid, &pos, 1);
if (sem < 0)
{
perror("semop");
return -1;
}
printf("ptr=%s\n", (char *)shmaddr);
}
return 0;
}