创建两个进程,定义一个共享内存,内存中存储char str[10]= "1234567";要求如下
A循环打印str;
B循环倒置str; 不能使用辅助数组;
要求出现的结果没有乱序,只能出现 1234567 7654321
不允许使用sleep函数
代码逆置
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
int main(int argc,const char * argv[])
{
int flag = 0;
key_t key = ftok ("./", 1);
if (key < 0)
{
perror ("ftok");
return -1;
}
int shmid = shmget (key, 128, IPC_CREAT|0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
void * addr = shmat (shmid, NULL, 0);
if ((void*)-1 == addr)
{
perror("shmat");
return -1;
}
*(int*)addr = 0;
char buf[8] = "1234567";
char *ptr = (char*)addr+1;
strcpy(ptr,buf);
char c;
while(1)
{
if (*(char*)addr == 0)
{
int low = 0;
int high = strlen(buf)-1;
while(low < high)
{
c = *((char*)addr+1+low);
*((char*)addr+1+low) = *((char*)addr+1+high);
*((char*)addr+1+high) = c;
low++;
high--;
}
//char *ptr = (char*)addr+1;
//strcpy(ptr,buf);
*(char*)addr = 1;
}
}
return 0;
}
打印
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
int main(int argc,const char * argv[])
{
key_t key = ftok ("./", 1);
if (key < 0)
{
perror ("ftok");
return -1;
}
int shmid = shmget (key, 128, IPC_CREAT|0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
void * addr = shmat (shmid, NULL, 0);
if ((void*)-1 == addr)
{
perror("shmat");
return -1;
}
//printf ("%d\n",*(int*)addr);
while(1)
{
if(*(char*)addr == 1)
{
printf ("%s\n",(char*)addr+1);
*(char*)addr = 0;
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
#include <semaphore.h>
struct msgbuf1 {
long mtype;
char mtext[128];
};
struct msgbuf2 {
long mtype;
char mtext[128];
};
struct msgbuf1 snd1;
struct msgbuf2 snd2;
sem_t sem1;
sem_t sem2;
void* Tid1 (void* arg)
{
key_t key = ftok("/home/ubuntu", 1);
if (key < 0)
{
perror("ftok");
//return NULL;
}
int msqid = msgget (key, IPC_CREAT|0664);
if (msqid < 0)
{
perror("msgget");
//return NULL;
}
while(1){
sem_wait(&sem1);
// printf ("%d\n", __LINE__);
printf ("请输入数据类型\n");
scanf ("%ld", &snd1.mtype);
// printf ("%d\n", __LINE__);
getchar();
//if (0 <= snd1.mtype)
//{
// break;
//}
printf ("请输入消息内容\n");
scanf ("%s", snd1.mtext);
if (strcasecmp(snd1.mtext, "quit") == 0)
{
break;
}
getchar();
if (msgsnd(msqid, &snd1, sizeof(snd1.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
//printf ("%d\n", __LINE__);
sem_post(&sem2);
}
msgctl(msqid, IPC_RMID, NULL);
system ("ipcs -q");
sem_destroy(&sem1);
sem_destroy(&sem2);
exit(0);
}
void* Tid2 (void* arg)
{
key_t key = ftok("/home/ubuntu", 1);
if (key < 0)
{
perror("ftok");
return NULL;
}
int msqid = msgget (key, IPC_CREAT|0664);
if (msqid < 0)
{
perror("msgget");
return NULL;
}
while(1)
{
sem_wait(&sem2);
//if( msgrcv(msqid, &snd, sizeof(snd.mtext), 4, 020000|IPC_NOWAIT) < 0)
if( msgrcv(msqid, &snd2, sizeof(snd2.mtext), 2, 0) < 0)
{
perror("msgrcv");
return NULL;
}else{
printf ("%s\n", snd2.mtext);
if (strcasecmp(snd2.mtext, "quit") == 0)
{
break;
}
}
sem_post(&sem1);
}
msgctl(msqid, IPC_RMID, NULL);
system ("ipcs -q");
sem_destroy(&sem1);
sem_destroy(&sem2);
exit(0);
}
int main(int argc,const char * argv[])
{
pthread_t tid1;
pthread_t tid2;
if (sem_init (&sem1, 0, 1) < 0)
{
perror("seminit");
return -1;
}
if (sem_init (&sem2, 0, 0) < 0)
{
perror("seminit");
return -1;
}
if (pthread_create(&tid1, NULL, Tid1, NULL) != 0)
{
printf ("lose __%d__", __LINE__);
return -1;
}
//pthread_detach(tid1);
if (pthread_create(&tid2, NULL, Tid2, NULL) != 0)
{
printf ("lose __%d__", __LINE__);
return -1;
}
//pthread_detach(tid2);
pthread_join (tid2, NULL);
pthread_join (tid1, NULL);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <pthread.h>
struct msgbuf1 {
long mtype;
char mtext[128];
};
struct msgbuf2 {
long mtype;
char mtext[128];
};
struct msgbuf1 snd1;
struct msgbuf2 snd2;
sem_t sem1;
sem_t sem2;
void* Tid1 (void* arg)
{
key_t key = ftok("/home/ubuntu", 1);
if (key < 0)
{
perror("ftok");
return NULL;
}
int msqid = msgget (key, IPC_CREAT|0664);
if (msqid < 0)
{
perror("msgget");
return NULL;
}
while(1)
{
sem_wait(&sem1);
//if( msgrcv(msqid, &snd, sizeof(snd.mtext), 4, 020000|IPC_NOWAIT) < 0)
if( msgrcv(msqid, &snd1, sizeof(snd1.mtext), 1, 0) < 0)
{
perror("msgrcv");
}else{
printf ("%s\n", snd1.mtext);
if (strcasecmp(snd1.mtext, "quit") == 0)
{
break;
}
}
sem_post(&sem2);
}
msgctl(msqid, IPC_RMID, NULL);
system ("ipcs -q");
sem_destroy(&sem1);
sem_destroy(&sem2);
exit(0);
}
void* Tid2 (void* arg)
{
//printf ("%d\n", __LINE__);
key_t key = ftok("/home/ubuntu", 1);
if (key < 0)
{
perror("ftok");
return NULL;
}
int msqid = msgget (key, IPC_CREAT|0664);
if (msqid < 0)
{
perror("msgget");
return NULL;
}
//printf ("%d\n", __LINE__);
while(1){
sem_wait(&sem2);
printf ("请输入数据类型\n");
scanf ("%ld", &snd2.mtype);
getchar();
//if (0 <= snd2.mtype)
//{
// break;
//}
printf ("请输入消息内容\n");
scanf ("%s", snd2.mtext);
getchar();
if (strcasecmp(snd2.mtext, "quit") == 0)
{
break;
}
if (msgsnd(msqid, &snd2, sizeof(snd2.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
sem_post(&sem1);
}
msgctl(msqid, IPC_RMID, NULL);
system ("ipcs -q");
sem_destroy(&sem1);
sem_destroy(&sem2);
exit(0);
}
int main(int argc,const char * argv[])
{
pthread_t tid1;
pthread_t tid2;
if (pthread_create(&tid1, NULL, Tid1, NULL) != 0)
{
printf ("lose __%d__", __LINE__);
return -1;
}
//pthread_detach(tid1);
if (pthread_create(&tid2, NULL, Tid2, NULL) != 0)
{
printf ("lose __%d__", __LINE__);
return -1;
}
//pthread_detach(tid2);
if (sem_init (&sem1, 0, 1) < 0)
{
perror("seminit");
return -1;
}
if (sem_init (&sem2, 0, 0) < 0)
{
perror("seminit");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}