zoukankan      html  css  js  c++  java
  • linux下的umask( )函数、setsid( )函数

    umask()函数:此函数的主要作用是在创建文件时设置或者屏蔽掉文件的一些权限。一般与open()函数配合使用。open函数原型:
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    int open( const char * pathname, int flags);
    int open( const char * pathname,int flags, mode_t mode);
    当创建一个文件并且要明确指定此文件的权限时,应该使用第二个open()函数,明确指定mode参数,所创建的文件最后的权限是:mode&(~mask)。默认的mask值是:022
    例:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>

    int main()
    {
            int fd;
    //      umask(0026);
            fd = open("/home/zhangcheng/share/test/test.txt",O_RDWR|O_CREAT,0666);
            if(fd < 0)
                    perror("open");
            return 0;
    }

    则生成的test.txt文件的权限是:666&(~026)结果是:-rw-r-----。如果没有umask(0026);这条语句,则生成的test.txt文件的权限是:666&(~022)结果是:-rw-r--r--。注:open函数的mode参数只有在创建文件时才有效。

          setsid( )说明:进程从它的双亲进程获得它的对话过程和进程组识别号。setsid()就是将进程和它当前的对话过程和进程组分离开,并且把它设置成一个新的对话过程的领头进程。
    pid_t pid = fork();
    if (pid == 0) {
            ...
            int result = execl(path, "adb", "fork-server", "server", NULL);
    } else {
            // run a program in a new session
            setsid();//之前parent和child运行在同一个session里,而且parent是session头,
            //所以作为session头的parent如果exit结束执行的话,那么会话session组中的所有进程将都被杀死;
            //所以执行setsid()之后,parent将重新获得一个新的会话session组id,child将仍持有原有的会话session组,
            //这时parent退出之后,将不会影响到child了。
    }
          会话session是一个或多个进程组的集合。进程调用setsid函数建立一个新会话。如果调用此函数的进程不是一个进程组的组长,则此函数就会创建一个新会话,该进程变成会话的首进程,然后该进程成为一个新进程组的组长进程,该进程没有控制终端。因为会话首进程是具有唯一进程ID的单个进程,所以可以将会话首进程的进程ID视为会话Id。
    sys.c中的创建一个会话的系统调用
    int sys_setsid(void)
    {
           ...
           current->leader = 1;
           current->session = current->pgrd = current->pid;  //创建该会话的进程号赋值成为会话号
           current->tty = -1;    //表示该领头进程没有控制终端。
           ...
    }

    参考原文:http://hi.chinaunix.net/?uid-20693307-action-viewspace-itemid-46100


  • 相关阅读:
    ora29861:域索引标记为loading/failed/unusable(转载)
    总遇到些莫名奇妙的问题,很不爽!
    dul 10恢复oracle lob数据方法(转载)
    C#用GDAL/OGR库创建与写Shape文件(转载)
    缺陷跟踪系统Mantis之安装篇(转载)
    Oracle10g闪回恢复区详细解析(转载)
    五大最受欢迎的BUG管理系统(转载)
    使用dul恢复数据(转载)
    DUL使用(转载)
    gdul 1.0.2 使用
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786962.html
Copyright © 2011-2022 走看看