作为技术版面的版主,回答问题应该让人感觉如沐春风,打不还手,骂不还口,可惜我的境界一直不够。
发信人: lcrystal (小水晶), 信区: Linux
标 题: 如何取得当前系统的进程
发信站: 天大求实BBS (Mon Mar 16 12:41:51 2009), 转信(bbs.tju.edu.cn)
要用c写个函数给java用,接受个参数为系统的进程名,取得当前系统的进程,看这个进程
名是否已经运行,是就返回true,不是返回false
现在的问题是,如何取得系统的所有进程名来比较,用ps的话,我目前只能想到定向到个文
件,然后再读文件,感觉有点不正规,有什么函数可以取得系统的所有进程,返回个
struct什么的,我知道在windows下有这样的函数,不过linux呢?
—
发信人: yudianzhiyu (yudianzhiyu), 信区: Linux
标 题: Re: 如何取得当前系统的进程
发信站: 天大求实BBS (Mon Mar 16 12:42:55 2009), 转信(bbs.tju.edu.cn)
找本关于linxu下编程的书看看
【 在 lcrystal 的大作中提到: 】
: 要用c写个函数给java用,接受个参数为系统的进程名,取得当前系统的进程,看这个进
程名是否已经运行,是就返回true,不是返回false
: 现在的问题是,如何取得系统的所有进程名来比较,用ps的话,我目前只能想到定向到个
文件,然后再读文件,感觉有点不正规,有什么函数可以取得系统的所有进程,返回个
struct什么的,我知道在windows下有这样的函数,不过linux呢?
—
发信人: darksun (大个 | 单身主义), 信区: Linux
标 题: Re: 如何取得当前系统的进程
发信站: 天大求实BBS (Mon Mar 16 23:17:41 2009), 转信(bbs.tju.edu.cn)
man一下kill
kill一下init应该就可以
ps:这种问题完全可以用google解决
【 在 lcrystal (小水晶) 的大作中提到: 】
:
: 要用c写个函数给java用,接受个参数为系统的进程名,取得当前系统的进程,看这个进
程名是否已经运行,是就返回true,不是返回false
—
发信人: lcrystal (小水晶), 信区: Linux
标 题: Re: 如何取得当前系统的进程
发信站: 天大求实BBS (Tue Mar 17 11:35:03 2009), 本站(bbs.tju.edu.cn)
楼上的真没劲,你g一个我看看,你知道windows下的如何写吗?你自己g一下看看,然后再
看看你说的那个和我要的一样吗?
—
于是乎,我决定如同愤青一样的骂回去,孔老夫子说:“以德报怨,何以报德。”所以我要骂回去,还要骂得有水平!
对解决该问题有兴趣的请参阅引用的两段材料,对8g有兴趣的请直接参阅最后一部分,看电视剧学英语最大的收获就是骂人。
I’m very upset when I finish my work today in 11.p.m. So I’m more upset when I see your words.
This is good answer for you, cause I want to yell sb. & you’re that guy, so I must give you sth. to start the yelling.
about the kill function in linux environment.
——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____
10.9. kill and raise Functions
The kill function sends a signal to a process or a group of processes. The raise function allows a process to send a signal to itself.
raise was originally defined by ISO C. POSIX.1 includes it to align itself with the ISO C standard, but POSIX.1 extends the specification of raise to deal with threads (we discuss how threads interact with signals in Section 12.8). Since ISO C does not
deal with multiple processes, it could not define a function, such as kill, that requires a process ID argument.
#include <signal.h>
int kill(pid_t pid, int signo);
int raise(int signo);
Both return: 0 if OK, 1 on error
The call
raise(signo);
is equivalent to the call
kill(getpid(), signo);
There are four different conditions for the pid argument to kill.
pid > 0
The signal is sent to the process whose process ID is pid.
pid == 0
The signal is sent to all processes whose process group ID equals the process group ID of the sender and for which the sender has permission to send the signal. Note that the term all processes excludes an implementation-defined set of system
processes. For most UNIX systems, this set of system processes includes the kernel processes and init (pid 1).
pid < 0
The signal is sent to all processes whose process group ID equals the absolute value of pid and for which the sender has permission to send the signal. Again, the set of all processes excludes certain system processes, as described earlier.
pid == 1
The signal is sent to all processes on the system for which the sender has permission to send the signal. As before, the set of processes excludes certain system processes.
As we’ve mentioned, a process needs permission to send a signal to another process. The superuser can send a signal to any process. For other users, the basic rule is that the real or effective user ID of the sender has to equal the real or effective
user ID of the receiver. If the implementation supports _POSIX_SAVED_IDS (as POSIX.1 now requires), the saved set-user-ID of the receiver is checked instead of its effective user ID. There is also one special case for the permission testing: if the
signal being sent is SIGCONT, a process can send it to any other process in the same session.
POSIX.1 defines signal number 0 as the null signal. If the signo argument is 0, then the normal error checking is performed by kill, but no signal is sent. This is often used to determine if a specific process still exists. If we send the process the
null signal and it doesn’t exist, kill returns 1 and errno is set to ESRCH. Be aware, however, that UNIX systems recycle process IDs after some amount of time, so the existence of a process with a given process ID does not mean that it’s the process
that you think it is.
Also understand that the test for process existence is not atomic. By the time that kill returns the answer to the caller, the process in question might have exited, so the answer is of limited value.
If the call to kill causes the signal to be generated for the calling process and if the signal is not blocked, either signo or some other pending, unblocked signal is delivered to the process before kill returns. (Additional conditions occur with
threads; see Section 12.8 for more information.)
——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____
—— This is from the APUE, I don’t know if this is the correct things you are looking for, but I think it can help you a little.
And if you want to get all process by C language, you can look this
——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____
/* vi: set sw=4 ts=4: */
/*
* Mini ps implementation(s) for busybox
*
* Copyright (C) 1999,2000 by Lineo, inc. Written by Erik Andersen
* <andersen@lineo.com>, <andersee@debian.org>
*
*
* This contains _two_ implementations of ps for Linux. One uses the
* traditional /proc virtual filesystem, and the other use the devps kernel
* driver (written by Erik Andersen to avoid using /proc thereby saving 100k+).
*
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include “internal.h”
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/ioctl.h>
#define BB_DECLARE_EXTERN
#define bb_need_help
#include “messages.c”
#define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */
#if ! defined BB_FEATURE_USE_DEVPS_PATCH
/* The following is the first ps implementation —
* the one using the /proc virtual filesystem.
*/
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
typedef struct proc_s {
char
cmd[16]; /* basename of executable file in call to exec(2) */
int
ruid, rgid, /* real only (sorry) */
pid, /* process id */
ppid; /* pid of parent process */
char
state; /* single-char code for process state (S=sleeping) */
} proc_t;
static int file2str(char *filename, char *ret, int cap)
{
int fd, num_read;
if ((fd = open(filename, O_RDONLY, 0)) == -1)
return -1;
if ((num_read = read(fd, ret, cap – 1)) <= 0)
return -1;
ret[num_read] = 0;
close(fd);
return num_read;
}
static void parse_proc_status(char *S, proc_t * P)
{
char *tmp;
memset(P->cmd, 0, sizeof P->cmd);
sscanf(S, “Name:\t%15c”, P->cmd);
tmp = strchr(P->cmd, ‘\n’);
if (tmp)
*tmp = ‘\0’;
tmp = strstr(S, “State”);
sscanf(tmp, “State:\t%c”, &P->state);
tmp = strstr(S, “Pid:”);
if (tmp)
sscanf(tmp, “Pid:\t%d\n” “PPid:\t%d\n”, &P->pid, &P->ppid);
else
fprintf(stderr, “Internal error!\n”);
/* For busybox, ignoring effective, saved, etc */
tmp = strstr(S, “Uid:”);
if (tmp)
sscanf(tmp, “Uid:\t%d”, &P->ruid);
else
fprintf(stderr, “Internal error!\n”);
tmp = strstr(S, “Gid:”);
if (tmp)
sscanf(tmp, “Gid:\t%d”, &P->rgid);
else
fprintf(stderr, “Internal error!\n”);
}
extern int ps_main(int argc, char **argv)
{
proc_t p;
DIR *dir;
FILE *file;
struct dirent *entry;
char path[32], sbuf[512];
char uidName[10] = “”;
char groupName[10] = “”;
int len, i, c;
#ifdef BB_FEATURE_AUTOWIDTH
struct winsize win = { 0, 0 };
int terminal_width = TERMINAL_WIDTH;
#else
#define terminal_width TERMINAL_WIDTH
#endif
if (argc > 1 && strcmp(argv[1], dash_dash_help) == 0) {
usage (“ps\n”
#ifndef BB_FEATURE_TRIVIAL_HELP
“\nReport process status\n”
“\nThis version of ps accepts no options.\n”
#endif
);
}
dir = opendir(“/proc”);
if (!dir)
fatalError(“Can’t open /proc\n”);
#ifdef BB_FEATURE_AUTOWIDTH
ioctl(fileno(stdout), TIOCGWINSZ, &win);
if (win.ws_col > 0)
terminal_width = win.ws_col – 1;
#endif
fprintf(stdout, “%5s %-8s %-3s %5s %s\n”, “PID”, “Uid”, “Gid”,
“State”, “Command”);
while ((entry = readdir(dir)) != NULL) {
uidName[0] = ‘\0’;
groupName[0] = ‘\0’;
if (!isdigit(*entry->d_name))
continue;
sprintf(path, “/proc/%s/status”, entry->d_name);
if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
parse_proc_status(sbuf, &p);
}
/* Make some adjustments as needed */
my_getpwuid(uidName, p.ruid);
if (*uidName == ‘\0’)
sprintf(uidName, “%d”, p.ruid);
my_getgrgid(groupName, p.rgid);
if (*groupName == ‘\0’)
sprintf(groupName, “%d”, p.rgid);
sprintf(path, “/proc/%s/cmdline”, entry->d_name);
file = fopen(path, “r”);
if (file == NULL)
continue;
i = 0;
len = fprintf(stdout, “%5d %-8s %-8s %c “, p.pid, uidName, groupName,
p.state);
while (((c = getc(file)) != EOF) && (i < (terminal_width-len))) {
i++;
if (c == ‘\0’)
c = ‘ ‘;
putc(c, stdout);
}
if (i == 0)
fprintf(stdout, “[%s]”, p.cmd);
fprintf(stdout, “\n”);
}
closedir(dir);
return(TRUE);
}
#else /* BB_FEATURE_USE_DEVPS_PATCH */
/* The following is the second ps implementation —
* this one uses the nifty new devps kernel device.
*/
#include <linux/devps.h> /* For Erik’s nifty devps device driver */
extern int ps_main(int argc, char **argv)
{
char device[] = “/dev/ps”;
int i, j, len, fd;
pid_t num_pids;
pid_t* pid_array = NULL;
struct pid_info info;
char uidName[10] = “”;
char groupName[10] = “”;
#ifdef BB_FEATURE_AUTOWIDTH
struct winsize win = { 0, 0 };
int terminal_width = TERMINAL_WIDTH;
#else
#define terminal_width TERMINAL_WIDTH
#endif
if (argc > 1 && **(argv + 1) == ‘-‘)
usage(“ps-devps\n\nReport process status\n\nThis version of ps accepts no options.\n\n”);
/* open device */
fd = open(device, O_RDONLY);
if (fd < 0)
fatalError( “open failed for `%s’: %s\n”, device, strerror (errno));
/* Find out how many processes there are */
if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
fatalError( “\nDEVPS_GET_PID_LIST: %s\n”, strerror (errno));
/* Allocate some memory — grab a few extras just in case
* some new processes start up while we wait. The kernel will
* just ignore any extras if we give it too many, and will trunc.
* the list if we give it too few. */
pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
pid_array[0] = num_pids+10;
/* Now grab the pid list */
if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
fatalError(“\nDEVPS_GET_PID_LIST: %s\n”, strerror (errno));
#ifdef BB_FEATURE_AUTOWIDTH
ioctl(fileno(stdout), TIOCGWINSZ, &win);
if (win.ws_col > 0)
terminal_width = win.ws_col – 1;
#endif
/* Print up a ps listing */
fprintf(stdout, “%5s %-8s %-3s %5s %s\n”, “PID”, “Uid”, “Gid”,
“State”, “Command”);
for (i=1; i<pid_array[0] ; i++) {
uidName[0] = ‘\0’;
groupName[0] = ‘\0’;
info.pid = pid_array[i];
if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
fatalError(“\nDEVPS_GET_PID_INFO: %s\n”, strerror (errno));
/* Make some adjustments as needed */
my_getpwuid(uidName, info.euid);
if (*uidName == ‘\0’)
sprintf(uidName, “%ld”, info.euid);
my_getgrgid(groupName, info.egid);
if (*groupName == ‘\0’)
sprintf(groupName, “%ld”, info.egid);
len = fprintf(stdout, “%5d %-8s %-8s %c “, info.pid, uidName, groupName, info.state);
if (strlen(info.command_line) > 1) {
for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) {
if (*(info.command_line+j) == ‘\0’ && *(info.command_line+j+1) != ‘\0’) {
*(info.command_line+j) = ‘ ‘;
}
}
*(info.command_line+j) = ‘\0′;
fprintf(stdout, “%s\n”, info.command_line);
} else {
fprintf(stdout, “[%s]\n”, info.name);
}
}
/* Free memory */
free( pid_array);
/* close device */
if (close (fd) != 0)
fatalError(“close failed for `%s’: %s\n”, device, strerror (errno));
exit (0);
}
#endif /* BB_FEATURE_USE_DEVPS_PATCH */
——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____
it’s the code of ps command in linux environment.
OK, here is the end of the good answers.
——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____——______—–_____—–_____
Let’s start the bad answer.
Don’t say thanks to me, I cost lots of time to answer this stupid question not for some stupid words which said by stupid people.
I don’t care how the shit Windows API do this function.
I don’t care what function you really want.
And I’m angry when I cost time looking for answer by google.com but received the words ” you’re boring so much.”
by the way, go to hell, “g.cn”.