您的位置:首页 > 运维架构 > Linux

linux c++ 多进程 匿名管道 命名管道 消息队列 信号量 共享内存 内存映射

2017-06-14 15:35 639 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <thread>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/mman.h>

//多进程
//匿名管道命名管道
//消息队列
//信号量
//共享内存
//内存映射

struct mymsg
{
long int msgtype;
char data[BUFSIZ];
};

union semun
{
int val;
struct semid_ds *buf;
unsigned short int *arry;
struct seminfo *__buf;
};

struct shmst
{
char name[20];
int age;
char str[100];
};

typedef struct shmst mmapst;

void onexit()
{
fprintf(stderr, "%s\n", "onexit");
}

void on_exit_cb(int status, void* arg)
{
fprintf(stderr, "on_exit_cb status = %d arg = %d \n", status, reinterpret_cast<size_t>(arg));
}

const char* buf = "hello ";

int main(int argc, char* argv[])
{
pid_t pid;
int fd;

//----------------------------
perror(argv[0]);
//----------------------------

//----------------------------
atexit(onexit);
on_exit(on_exit_cb, (void*)1);
//----------------------------

//----------------------------
fd = open("a.txt", O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
//----------------------------

//----------------------------
//管道
int _pipe[2] = { 0 };
int ret = pipe(_pipe);
if (ret == -1)
{
printf("pipe error %d", errno, strerror(errno));

exit(-1);
}
//----------------------------

//----------------------------
//消息队列
std::thread t([] {
int msgid = msgget(1234, IPC_CREAT | 0666);
if(msgid == -1)
perror("msgget");
struct mymsg msg2 = { 0 };
while (true)
{
ssize_t ret = msgrcv(msgid, &msg2, sizeof(mymsg) - sizeof(long int), 0, 0/*IPC_NOWAIT*/);
if (ret == -1)
{
perror("msgrcv error");
}
else if(ret > 0)
{
if (strcmp(msg2.data, "end") == 0)
{
printf("msgrec msg:%s\n", msg2.data);
break;
}
}
}

msgctl(msgid, IPC_RMID, 0);
});

std::this_thread::sleep_for(std::chrono::seconds(2));
//----------------------------

unlink("./testsem.ini");
if (open("./testsem.ini", O_RDWR | O_CREAT | O_TRUNC, 0666) == -1)
{
perror("open");
}

//----------------------------
//信号量
key_t sem_key = ftok("./testsem.ini", 1);
perror("ftok");
int sem_id = semget(sem_key, 1, 0666 | IPC_CREAT);
if (sem_id == -1)
{
perror("semget");
}

union semun sem_union;
sem_union.val = 1;

if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
{
perror("semctl");
}
//----------------------------

//----------------------------
//共享内存

int shm_id = shmget(key_t(12345), sizeof(struct shmst), 0666 | IPC_CREAT);
if (shm_id == -1)
perror("shmget");

void* pbuf = shmat(shm_id, 0, 0);
if (pbuf == NULL)
perror("shmat");
else
{
struct shmst* pshmst = (struct shmst*)pbuf;
strcpy(pshmst->name, "lls");
strcpy(pshmst->str, "hello lls");
pshmst->age = 27;

if (shmdt(pbuf) == -1)
perror("shmdt");
}
//----------------------------

//----------------------------
//内存映射
int mmap_fd = open("./mmap.temp", O_CREAT | O_RDWR | O_TRUNC, 0666);
if (mmap_fd == -1)
perror("open");

write(mmap_fd, " ", sizeof(mmapst));//不加这句会报bus error

mmapst* pmmapbuf = (mmapst*)mmap(NULL, sizeof(mmapst), PROT_READ | PROT_WRITE, MAP_SHARED, mmap_fd, 0);
if (pmmapbuf == NULL)
perror("mmap");

strcpy(pmmapbuf->name, "lls");
strcpy(pmmapbuf->str, "hello lls");
pmmapbuf->age = 27;

if (msync(pmmapbuf, sizeof(mmapst), MS_SYNC) == -1)
perror("msync");

if (munmap(pmmapbuf, sizeof(mmapst)) == -1)
perror("munmap");

close(mmap_fd);
//----------------------------

pid = fork();
if (pid == -1)
{
printf("%s", "fork error\n");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
//----------------------------
close(_pipe[0]);
write(_pipe[1], "abcde", 5);
//----------------------------

//----------------------------
write(fd, buf, strlen(buf));
close(fd);
//----------------------------

if (-1 == execl("/home/lls/projects/testfile/bin/x64/Debug/testfile.out", "lls", "luls", NULL))
{
perror("execl error");
printf("execl error: %d: %s\n", errno, strerror(errno));
}

//execlp("ls", "l", NULL);

//execle("ls", "l", NULL, "/bin");

//char* const argv[] = {"ls", NULL};
//execv("/bin/ls", argv);

//execvp("ls", argv);

//char* const envp[] = { "/bin", NULL };
//execve("ls", argv, envp);

//printf("i'm child child_pid = %d ppid = % d!\n", getpid, getppid);
//exit(110);
}

//----------------------------
int s = access("./temp.txt", 0);
if (s != 0)
{
ret = mkfifo("./temp.txt", 0666 | __S_IFIFO);
if (ret == -1)
{
perror("mkfifo error");
}
}

int fifofd = open("./temp.txt", O_RDONLY);
char* szb[10] = { 0 };
read(fifofd, szb, 5);
close(fifofd);

printf("------------------------\n");
printf("fifo:\n");
fprintf(stderr, "read fifo : %s\n", szb);
printf("------------------------\n");

//----------------------------

struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
perror("semop");
}

//----------------------------
close(_pipe[1]);
char szbuf[10] = { 0 };
read(_pipe[0], szbuf, 5);

printf("------------------------\n");
printf("pipe:\n");
fprintf(stderr, "read pipe : %s\n", szbuf);
printf("------------------------\n");
//----------------------------

printf("i'm parent pid = %d!\n", getpid());

//----------------------------
int status = 0;
waitpid(pid, &status, 0);
//----------------------------

//----------------------------
lseek(fd, 0, SEEK_SET);
char buf2[100] = { 0 };
ssize_t res = read(fd, (void*)buf2, 100);
(void)res;
close(fd);
printf("%s\n", buf2);
//----------------------------

//----------------------------
t.join();
//----------------------------

//----------------------------
//struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
perror("semop");
}

if (semctl(sem_id, 0, IPC_RMID, 0) == -1)
perror("semctl");
//----------------------------

//----------------------------
if (shmctl(shm_id, IPC_RMID, 0) == -1)
{
perror("shmctl");
}
//----------------------------

return 0;
}


#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/mman.h>

struct mymsg
{
long int msgtype;
char data[BUFSIZ];
};

union semun
{
int val;
struct semid_ds* buf;
unsigned short int* array;
struct seminfo *__buf;
};

typedef struct shmst
{
char name[20];
int age;
char str[100];
}mmapst;

int main(int argc, char* argv[])
{
key_t sem_key = ftok("./testsem.ini", 1);
int sem_id = semget(sem_key, 1, 0666 | IPC_CREAT);
if (sem_id == -1)
{
perror("semget");
}

/*union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
{
perror("semctl");
}*/

struct sembuf sem_bf;
sem_bf.sem_num = 0;
sem_bf.sem_op = -1;
sem_bf.sem_flg = SEM_UNDO;

if (semop(sem_id, &sem_bf, 1) == -1)
{
perror("semop");
}

printf("------------------------\n");

//----------------------------
//共享内存

int shm_id = shmget(key_t(12345), sizeof(struct shmst), 0666 | IPC_CREAT);
if (shm_id == -1)
perror("shmget");

void* pbuf = shmat(shm_id, 0, 0);
if (pbuf == NULL)
perror("shmat");
else
{
struct shmst* pshmst = (struct shmst*)pbuf;

printf("------------------------\n");
printf("shm:\n");
printf("name:%s\n", pshmst->name);
printf("str:%s\n", pshmst->str);
printf("age:%d\n", pshmst->age);
printf("------------------------\n");

if (shmdt(pbuf) == -1)
perror("shmdt");
}

//----------------------------

//----------------------------
//内存映射
int mmap_fd = open("./mmap.temp", O_RDONLY);
if (mmap_fd == -1)
perror("open");

mmapst* pmmapbuf = (mmapst*)mmap(NULL, sizeof(mmapst), PROT_READ, MAP_SHARED, mmap_fd, 0);
if (pmmapbuf == NULL)
perror("mmap");

printf("------------------------\n");
printf("mmap:\n");
printf("name:%s\n", pmmapbuf->name);
printf("str:%s\n", pmmapbuf->str);
printf("age:%d\n", pmmapbuf->age);
printf("------------------------\n");

if (munmap(pmmapbuf, sizeof(mmapst)) == -1)
perror("munmap");

close(mmap_fd);
//----------------------------

key_t key = 1234;
int msgid = msgget(key, 0666
4000
| IPC_CREAT);
printf("msgget msgid = %d\n", msgid);

struct mymsg msg = { 0 };
msg.msgtype = 1;
strcpy(msg.data, "end");

if (-1 == msgsnd(msgid, &msg, sizeof(mymsg) - sizeof(long int), 0))
{
perror("msgsend error");
}

printf(argv[0]);
printf("\n");
printf(argv[1]);
printf("\n");
printf("hello from testfile!\n");

char path[1024] = { 0 };
getcwd(path, 1024);
printf(path);
printf("\n");

int s = access("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0);
if (s == 0)
{
//unlink("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt");
}
else
{
int ret = mkfifo("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0666 | __S_IFIFO);
if (ret == -1)
{
perror("mkfifo error");
}
}

int fifofd = open("./temp.txt", O_WRONLY);
write(fifofd, "edcba", 5);
close(fifofd);

printf("------------------------\n");

sem_bf.sem_num = 0;
sem_bf.sem_op = 1;
sem_bf.sem_flg = SEM_UNDO;

if (semop(sem_id, &sem_bf, 1) == -1)
{
perror("semop");
}

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐