[C++] 纯文本查看 复制代码
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main()
{
printf("PID=%ld, PGID=%ld, SID=%ld\n", (long)getpid(), (long)getpgrp(), getsid(0));
if (fork() != 0)
_exit( EXIT_SUCCESS ); //错误或父进程都退出
if (setsid() == -1) {
perror("setsid");
exit(EXIT_FAILURE);
}
printf("PID=%ld, PGID=%ld, SID=%ld\n", (long)getpid(), (long)getpgrp(), getsid(0));
if (open("/dev/tty", O_RDWR) == -1) {
perror("open");
exit (EXIT_FAILURE);
}
return 0;
}
[C++] 纯文本查看 复制代码
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main()
{
printf("PID=%ld, PGID=%ld, SID=%ld\n", (long)getpid(), (long)getpgrp(), getsid(0));
if (fork() != 0)
_exit( EXIT_SUCCESS ); //错误或父进程都退出
if (setsid() == -1) {
perror("setsid");
exit(EXIT_FAILURE);
}
printf("PID=%ld, PGID=%ld, SID=%ld\n", (long)getpid(), (long)getpgrp(), getsid(0));
if (open("/dev/tty", O_RDWR) == -1) {
perror("open");
// exit (EXIT_FAILURE);
}
sleep(10);
printf ("Not accept your command.. bye\n");
return 0;
}