23,216
社区成员




#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
int back = 0;
void f1(int a) { back = 1; }
void f2(int a){};
main()
{
char a;
int fd;
signal(SIGTTIN, f1);
signal(SIGALRM, f2);
fd = open("/dev/tty", O_RDWR);
alarm(1);
read(fd, &a, 1);
alarm(0);
if( back == 1 ) printf("oo, background\n");
else printf("oo, foreground\n");
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char **argv)
{
FILE *fp;
char fmt[FILENAME_MAX] = {0};
char buf[BUFSIZ] = {0};
sprintf (fmt, "ps ef | grep %s | awk '{print $3}'", argv[0]);
fp = popen (fmt, "r");
while (fgets (buf, BUFSIZ, fp) != NULL) {
fgets (buf, BUFSIZ, fp);
if (!strrchr (buf, '+'))
printf ("Bg: %s", buf);
else
printf ("Fg: %s", buf);
}
pclose (fp);
}