posts - 297,  comments - 15,  trackbacks - 0
process进程 and thread
获取当前进程getpid(),父进程getppid(),当前用户ID,getuid(), geteuid(),组ID,getgid(),getegid(), all need head file <unistd.h>
getlogin()返回登录用户名
example:
#include <iostream.h>
#include <unistd.h> //getpid() getppid()
int main()
{
cout<<getpid()<<endl; //current process ID
cout<<getppid()<<endl; //parent process ID
cout<<getgid()<<endl; //group process ID
cout<<getegid()<<endl; //effective group process ID
cout<<getuid()<<endl; //user process ID
cout<<geteuid()<<endl; //effective user process ID
cout<<getlogin()<<endl; //getlogin() return the login user name
                                                                            
return 0;
}

获取登录用户的个人信息,如用户名,当前目录,用户ID,组ID等,need function struct passwd * getpwnam(const char *name)
如下面的例子:
/*
 * getname.c - Get login names
 */
#include <stdio.h>
#include <stdlib.h>//exit()
#include <unistd.h>//getlogin()
#include <pwd.h> //getpwnam()
                                                                                
int main(void)
{
        char *login;
        struct passwd *pentry;
                                                                                
        /* Get the login name */
        if((login = getlogin()) == NULL) { /* oops */
        perror("getlogin");
        exit(EXIT_FAILURE);
        }
                                                                                
        /* Get the password entry for login */
        if((pentry = getpwnam(login)) == NULL) {
        perror("getpwnam");
       exit(EXIT_FAILURE);
        }
                                                                                
        /* Display the password entry */
        printf("user name: %s\n", pentry->pw_name);
        printf("UID : %d\n", pentry->pw_uid);
        printf("GID : %d\n", pentry->pw_gid);
        printf("gecos : %s\n", pentry->pw_gecos);
        printf("home dir : %s\n", pentry->pw_dir);
        printf("shell : %s\n", pentry->pw_shell);
                                                                                
        exit(EXIT_SUCCESS);
}

system()
如果没有找到/bin/sh则返回127,成功返回0,出错返回-1
example:
/*
 * system.c - Demonstrate the system() call
 */
#include <stdio.h>
#include <stdlib.h>
                                                                                
int main(void)
{
        int retval;
                                                                                
        retval = system("ls -l");
                                                                                
        if(retval == 127) {
                fprintf(stderr, "/bin/sh not available\n");
                exit(127);
        } else if(retval == -1) {
                perror("system");
                exit(EXIT_FAILURE);
        } else if(retval != 0) {
                fprintf(stderr, "command returned %d\n", retval);
                perror("ls");
        } else {
                puts("command successfully executed");
}
        exit(EXIT_SUCCESS);
}

fork调用创建一个新进程
#include <unistd.h>

pid_t fork(void);

Description
fork() creates a child process that differs from the parent process only in its PID and PPID,
return value:
On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately.执行成功,就向父进程返回子进程的PID,并向子进程返回0,只调用一次fork,它会返回两次

example:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
                                                                                
int main(void)
{
    pid_t child;
                                                                                
    if((child = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    } else if(child == 0) {
        puts("in child");
        printf("\tchild pid = %d\n", getpid());
        printf("\tchild ppid = %d\n", getppid());
        exit(EXIT_SUCCESS);
    } else {
        puts("in parent");
        printf("\tparent pid = %d\n", getpid());
        printf("\tparent ppid = %d\n", getppid());
    }
    exit(EXIT_SUCCESS);
}

exec()函数族
exec用被执行的程序完全替换了调用进程的映像。exec启动一个新程序,替换原有的进程
execl, execlp, execle, execv, execvp - execute a file
Synopsis
#include <unistd.h>

extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,
..., char * const envp[]);
int execve    (const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
以上的函数都必须以NULL结束

example:
/*
 * execve.c - Illustrate execve
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
                                                                                
int main(void)
{
    char *argv[] = {"/bin/ls", NULL};
                                                                                
    if(execve("/bin/ls", argv, NULL) == -1) {
        perror("execve");
        exit(EXIT_FAILURE);
    }
                                                                                
    puts("shouldn'
t get here");
                                                                                
    exit(EXIT_SUCCESS);
}
使用wait and waitpid调用可以收集子进程的退出状态
Name
wait, waitpid - wait for process to change state
Synopsis
#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id "
, siginfo_t *" infop ", int " options );
Description
All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. In the case of a terminated child, performing a wait allows the system to release the resources associated with the child; if a wait is not performed, then terminated the child remains in a "
zombie" state (see NOTES below).

If a child has already changed state, then these calls return immediately. Otherwise they block until either a child changes state or a signal handler interrupts the call (assuming that system calls are not automatically restarted using the SA_RESTART flag of sigaction(2)). In the remainder of this page, a child whose state has changed and which has not yet been waited upon by one of these system calls is termed waitable.
wait() and waitpid()
The wait() system call suspends execution of the current process until one of its children terminates. The call wait(&status) is equivalent to:

waitpid(-1, &status, 0);

The waitpid() system call suspends execution of the current process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behaviour is modifiable via the options argument, as described below.

The value of pid can be:

< -1
    meaning wait for any child process whose process group ID is equal to the absolute value of pid.
-1
    meaning wait for any child process.
0
    meaning wait for any child process whose process group ID is equal to that of the calling process.
> 0
    meaning wait for the child whose process ID is equal to the value of pid.

The value of options is an OR of zero or more of the following constants:

WNOHANG
    return immediately if no child has exited.
WUNTRACED
    also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified.
WCONTINUED
    (Since Linux 2.6.10) also return if a stopped child has been resumed by delivery of SIGCONT.
if you want to more information aboat it, you can reference http://linux.die.net.

example:
/*
 * waiter.c - Simple wait usage
 */
#include <unistd.h>
//#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
                                                                                
int main(void)
{
    pid_t child;
    int status;
                                                                                
    if((child = fork()) == -1) {
        perror("
fork");
        exit(EXIT_FAILURE);
    } else if(child == 0) {
        puts("
in child");
        printf("
\tchild pid = %d\n", getpid());
        printf("
\tchild ppid = %d\n", getppid());
        exit(EXIT_SUCCESS);
    } else {

        /* Wait for the child to exit */
        waitpid(child, &status, 0);
        printf("
in parent\n");
        printf("
\tparent pid = %d\n", getpid());
        printf("
\tparent ppid = %d\n", getppid());
        printf("
\tchild exited with %d\n", status);
    }
    exit(EXIT_SUCCESS);
}

abort()
#include <stdlib.h>
void abort(void)

kill()
#include <signal.h>
#include <sys/types.h>
int kill(pid_t pid, int sig)

example:
~/*
 * killer.c - Killing other processes
 */
#include <sys/types.h>
#include <sys/wait.h>//waitpid()
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>//signal
                                                                                
int main(void)
{
    pid_t child;
    int status, retval;
                                                                                
    if((child = fork()) < 0) {
        perror("
fork");
        exit(EXIT_FAILURE);
    }
    if(child == 0) {
        /* Sleep long enough to be killed */
        
         sleep(1000);
        exit(EXIT_SUCCESS);
    } else {
        /* Use WNOHANG so wait will return */
        if((waitpid(child, &status, WNOHANG)) == 0) {
            retval = kill(child, SIGKILL);
            if(retval) {
                /* Kill failed, so wait on child to exit */
                puts("
kill failed");
                perror("
kill");
                waitpid(child, &status, 0);
            } else
                printf("
%d killed\n", child);
        }
    }
    exit(EXIT_SUCCESS);
}
~
信 号:
alarm(), pause(),kill(), sigfillset(),sigaction(),sigpending(),all the functions can find from the site http://linux.die.net

线程
_clone(), pthread_creat(), pthread_exit(),pthread_join(),pthread_atfork(),pthread_cancel(), pthread cleanup宏,pthread_equal()
线程互斥,pthread_mutex_init, pthread_mutex_lock
pthead 中的p display posix

from:
http://blog.chinaunix.net/u1/45689/showart_689217.html
posted on 2010-03-14 15:09 chatler 阅读(496) 评论(0)  编辑 收藏 引用 所属分类: Linux_Coding

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜