#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include "test.h"
//多进程
//匿名管道命名管道
//消息队列
//信号量
//共享内存
//内存映射
//文件文件夹
//网络
//时间定时器
void onexit()
{
fprintf(stderr, "%s\n", "onexit");
}
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);
}
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);
}
close(_pipe[1]);
char szbuf[10] = { 0 };
read(_pipe[0], szbuf, 5);
fprintf(stderr, "read pipe : %s\n", szbuf);
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);
fprintf(stderr, "read fifo : %s\n", szb);
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);
close(fd);
printf("%s\n", buf2);
return 0;
}
void on_exit_cb(int status, void* arg)
{
fprintf(stderr, "on_exit_cb status = %d arg = %d \n", status, (int)arg);
}
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
printf("------------------------\n");
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");
return 0;
}