函数,它们的分工和人类的分工没什么不同----小话c语言(9)

[Mac-10.7.1 Lion Intel-based x64 gcc4.2.1]


Q: 对于ctype.h中的isspace和isblank函数,一直没怎么分清楚,到底它们的不同在哪里?

A: 我们做个测试:

#include <stdio.h>
#include <ctype.h>

int main()
{
    int i;
    for(i = 0; i < 128; ++i)
    {
        if(isspace(i))
            printf("%d is space\n", i);
        if(isblank(i))
            printf("%d is blank\n", i);
    }
    return 0;
}

编译运行:

可以看到ascii码9, 10, 11, 12, 13, 32是space;  9, 32是blank.

查看isspace和isblank说明:


Q: 字符串转换成浮点数可以用atof, 那浮点数转换成字符串呢?

A: 可以使用sprintf函数组装字符串,也可以用gcvt函数实现。如下例子:

#include <stdio.h>
#define	PRINT_D(intValue)	printf(#intValue" is %lu\n", (intValue));

int main()
{
    char buf[32];
    sprintf(buf, "%lf", 2.345);
    printf("%s\n", buf);
    
    return 0;
}
运行结果:


也可以用gcvt函数: 

char *gcvt(double value, int ndigit, char *buf);

其中ndigit是有效数字个数,buf为保存转换后的字符串;

如下例子:

#include <stdio.h>
#include <stdlib.h>
#define	PRINT_D(intValue)	printf(#intValue" is %lu\n", (intValue));

int main()
{
    char buf[32];
    double d = 1.234567;
    gcvt(d, 6, buf);
    printf("%s\n", buf);
    
    return 0;
}
运行结果:

另外,ecvt, fcvt也可以实现类似功能。


Q: 在申请内存的时候,经常申请ok了,然后调用memset将内存空间设置为0,有没有更简洁的形式?

A: 有的,calloc可以完成这个功能。

原型为:

void *calloc(size_t count, size_t size);

如下代码:

#include <stdio.h>
#include <stdlib.h>
#define	PRINT_D(intValue)	printf(#intValue" is %lu\n", (intValue));

int main()
{
    char *p = (char *)calloc(128, 1);
    if(p)
    {
        int i = 0;
        for(; i < 128; ++i)
        {
            if(p[i] != 0)
                printf("the calloc memory is not zero!\n");
        }
        free(p);
    }
    
    return 0;
}
当然,正常情况下,它什么也不会输出。


Q: 经常看到,申请一块缓存,大小为4KB,它就是系统的一页大小么?系统有相关的api么?

A: 有相关api.  getpagesize即可获取它的大小。

int  getpagesize(void);
如下例子:
#include <stdio.h>
#include <unistd.h>
#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));

int main()
{
    int page_size = getpagesize();
    PRINT_D(page_size)
    
    return 0;
}
运行结果:


Q: 经常听到内存映射,它的作用到底是什么?

A: mmap函数就可以实现内存映射。它的用途之一就是当需要频繁操作文件的时候,可以将文件映射到内存中,在内存中读写这些可以提高效率。原型如下:

void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);

它需要头文件sys/mman.h的支持。

示例代码如下:

addr表示被映射的地址,可以传入NULL,返回值是系统分配的内存映射地址;

len表示文件多大的数据被映射到内存;

prot表示映射的读写或可执行方式,如PROT_READ是以读的方式;

flags表示映射的一些特性,比如MAP_SHARED表示允许其它映射此文件的进程共享;

fd表示映射的文件句柄;

offset表示文件映射的偏移量,必须是分页大小的整数倍;

现在写一个测试代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));

int main()
{
    int     ret;
    char    *map_p;
    struct  stat st;
    
    long long     size = getpagesize();
    // open file by read and write, the write property is used when modifying the mapped data
    int     fd = open("test", O_RDWR);
    if(fd < 0)
    {
        perror("open file error");
        return -1;
    }
    ret = fstat(fd, &st);
    if(ret < 0)
    {
        perror("fstat file error");
        close(fd);
        return -1;
    }
    
    // if the size is smaller than file's size, then use the file (size + 1)
    if(st.st_size >= size)
        size = st.st_size + 1;
    
    // map the file fd
    map_p = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(map_p == MAP_FAILED)
    {
        perror("mmap error");
        close(fd);
        return -1;
    }
    close(fd);  // when mapped ok, the fd can be closed.
    
    printf("file content:%s\n", map_p);
    // modify the first char
    map_p[0] = 'a';
    
    // sync the modify data
    ret = msync(map_p, size, MS_SYNC);
    if(ret < 0)
    {
        perror("msync error");
        munmap(map_p, size);
        return -1;
    }
    

    printf("modify file content:%s\n", map_p);
    // unmap
    munmap(map_p, size);
    return 0;
}

我们创建一个含有hello内容的文件:

-n参数表示不把最后的换行字符\n也写入文件;

用ls -l | grep test命令确认文件内容为5字节:

可以看到确定是5字节。

然后运行上面的程序,观察控制台输出:

用cat  test命令确认文件内容已被修改:


Q: 上面的代码中map_p用char *输出,结尾不是没设定\0么,怎么会正确结束?

A: 这是因为进行mmap内存映射的时候,会自动将映射后所有没有被映射的空间填充0,所以代码中没有显式设置。


Q: 关于时间的几个函数,感觉不容易分清楚,究竟如何理解它们?

A: 首先说下time函数:

time_t  time(time_t *tloc);
它返回的是1970年1月1日的UTC时间从0时0分0秒到现在的秒数。如果tloc非空,那么它也会存储在tloc对应的地址里。

time_t格式一般被定义为long或者unsigned long这种格式。

一个例子:

#include <stdio.h>
#include <time.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));

int main()
{
    time_t  ret = time(NULL);
    PRINT_L(ret)
    return 0;
}

运行结果:


而这种格式的时间当然不直观,所以有localtime来转换:

struct tm *localtime(const time_t *clock);

它返回一个tm结构的指针。可以利用time得到的返回值作为参数:

#include <stdio.h>
#include <time.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));

int main()
{
    time_t  ret = time(NULL);
    struct tm *tm_ret;
    
    PRINT_L(ret)
    
    tm_ret = localtime(&ret);
    PRINT_D(tm_ret->tm_year)
    PRINT_D(tm_ret->tm_mon)
    PRINT_D(tm_ret->tm_mday)
    PRINT_D(tm_ret->tm_hour)
    PRINT_D(tm_ret->tm_min)
    PRINT_D(tm_ret->tm_sec)
    
    return 0;
}

运行结果:


Q: 刚刚是time_t结构转换成struct tm结构,有相反的过程么?

A: 是的,可以使用mktime函数实现这个转换。

time_t mktime(struct tm *timeptr);

就利用上面的代码:

#include <stdio.h>
#include <time.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));

int main()
{
    time_t  ret = time(NULL);
    struct tm *tm_ret;
    
    PRINT_L(ret)
    
    tm_ret = localtime(&ret);
    PRINT_D(tm_ret->tm_year)
    PRINT_D(tm_ret->tm_mon)
    PRINT_D(tm_ret->tm_mday)
    PRINT_D(tm_ret->tm_hour)
    PRINT_D(tm_ret->tm_min)
    PRINT_D(tm_ret->tm_sec)
    
    PRINT_L(mktime(tm_ret))
    
    return 0;
}

运行结果:

可以看到,mktime返回的和程序开始time函数得到的time_t的值是一样的。


Q: 使用struct tm 结构来获取年月日时分秒,感觉有点复杂,有更简单的么?

A: 有的,可以使用ctime和asctime来得到时间相关的char *字符串,这个要更简洁一点。

char *ctime(const time_t *clock);
char *asctime(const struct tm *timeptr);

可以看到,这两个函数有不同之处,参数不同。示例代码:

#include <stdio.h>
#include <time.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

int main()
{
    time_t  ret = time(NULL);
    struct tm *tm_ret;
    
    PRINT_L(ret)
    
    tm_ret = localtime(&ret);
    PRINT_D(tm_ret->tm_year)
    PRINT_D(tm_ret->tm_mon)
    PRINT_D(tm_ret->tm_mday)
    PRINT_D(tm_ret->tm_hour)
    PRINT_D(tm_ret->tm_min)
    PRINT_D(tm_ret->tm_sec)
    
    PRINT_L(mktime(tm_ret))
    
    PRINT_STR(ctime(&ret))
    PRINT_STR(asctime(tm_ret))
    
    return 0;
}
运行结果:



Q: 关于内存拷贝,memcpy和memmove到底有什么区别?

A: 区别在于memmove处理了重复内存区域的拷贝,memcpy没有处理内存区域重复可能导致拷贝结果错误,这种情况下结果是不确定的。所以最好使用memmove,如果可以确定内存区域不重复,也可以直接使用memcpy.


Q: 关于系统信息,如何获取系统的用户组信息?

A: getgrent函数可以实现。

group结构如下:

struct group {
	char	*gr_name;		/* [XBD] group name */
	char	*gr_passwd;		/* [???] group password */
	gid_t	gr_gid;			/* [XBD] group id */
	char	**gr_mem;		/* [XBD] group members */
};

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

int main()
{
    struct group *data;
    while(data = getgrent())
    {
        printf("%s:%s:%d\n", data->gr_name, data->gr_passwd, data->gr_gid);
    }
    endgrent();
    return 0;
}

运行结果(不同系统的结果会不同):

_amavisd:*:83
_appowner:*:87
_appserveradm:*:81
_appserverusr:*:79
_appstore:*:33
_ard:*:67
_atsserver:*:97
_calendar:*:93
_carddav:*:206
_ces:*:32
_clamav:*:82
_coreaudiod:*:202
_cvms:*:212
_cvs:*:72
_detachedsig:*:207
_devdocs:*:59
_developer:*:204
_devicemgr:*:220
_dovenull:*:227
_dpaudio:*:215
_guest:*:201
_installassistant:*:25
_installer:*:96
_jabber:*:84
_keytabusers:*:30
_lda:*:211
_locationd:*:205
_lp:*:26
_lpadmin:*:98
_lpoperator:*:100
_mailman:*:78
_mcxalr:*:54
_mdnsresponder:*:65
_mysql:*:74
_netbios:*:222
_netstatistics:*:228
_networkd:*:24
_odchpass:*:209
_pcastagent:*:55
_pcastfeedadmins:*:223
_pcastlibrary:*:225
_pcastserver:*:56
_podcastlibraryfeedadmins:*:226
_postdrop:*:28
_postfix:*:27
_postgres:*:216
_qtss:*:76
_sandbox:*:60
_screensaver:*:203
_scsd:*:31
_securityagent:*:92
_serialnumberd:*:58
_softwareupdate:*:200
_spotlight:*:89
_sshd:*:75
_svn:*:73
_teamsserver:*:94
_timezone:*:210
_tokend:*:91
_trustevaluationagent:*:208
_unknown:*:99
_update_sharing:*:95
_usbmuxd:*:213
_uucp:*:66
_warmd:*:224
_webauthserver:*:221
_windowserver:*:88
_www:*:70
_xgridagent:*:86
_xgridcontroller:*:85
access_bpf::501
accessibility:*:90
admin:*:80
authedusers:*:50
bin:*:7
certusers:*:29
com.apple.access_screensharing-disabled::101
com.apple.access_screensharing::401
com.apple.sharepoint.group.1::402
com.apple.sharepoint.group.2::403
consoleusers:*:53
daemon:*:1
dialer:*:68
everyone:*:12
group:*:16
interactusers:*:51
kmem:*:2
localaccounts:*:61
macports::502
mail:*:6
messagebus:*:500
netaccounts:*:62
netusers:*:52
network:*:69
nobody:*:-2
nogroup:*:-1
operator:*:5
owner:*:10
procmod:*:9
procview:*:8
smmsp:*:102
staff:*:20
sys:*:3
tty:*:4
utmp:*:45
wheel:*:0
nobody:*:-2
nogroup:*:-1
wheel:*:0
daemon:*:1
kmem:*:2
sys:*:3
tty:*:4
operator:*:5
mail:*:6
bin:*:7
procview:*:8
procmod:*:9
owner:*:10
everyone:*:12
group:*:16
staff:*:20
_networkd:*:24
_installassistant:*:25
_lp:*:26
_postfix:*:27
_postdrop:*:28
certusers:*:29
_keytabusers:*:30
_scsd:*:31
_ces:*:32
_appstore:*:33
utmp:*:45
authedusers:*:50
interactusers:*:51
netusers:*:52
consoleusers:*:53
_mcxalr:*:54
_pcastagent:*:55
_pcastserver:*:56
_serialnumberd:*:58
_devdocs:*:59
_sandbox:*:60
localaccounts:*:61
netaccounts:*:62
_mdnsresponder:*:65
_uucp:*:66
_ard:*:67
dialer:*:68
network:*:69
_www:*:70
_cvs:*:72
_svn:*:73
_mysql:*:74
_sshd:*:75
_qtss:*:76
_mailman:*:78
_appserverusr:*:79
admin:*:80
_appserveradm:*:81
_clamav:*:82
_amavisd:*:83
_jabber:*:84
_xgridcontroller:*:85
_xgridagent:*:86
_appowner:*:87
_windowserver:*:88
_spotlight:*:89
accessibility:*:90
_tokend:*:91
_securityagent:*:92
_calendar:*:93
_teamsserver:*:94
_update_sharing:*:95
_installer:*:96
_atsserver:*:97
_lpadmin:*:98
_unknown:*:99
_lpoperator:*:100
_softwareupdate:*:200
_guest:*:201
_coreaudiod:*:202
_screensaver:*:203
_developer:*:204
_locationd:*:205
_detachedsig:*:207
_trustevaluationagent:*:208
_odchpass:*:209
_timezone:*:210
_lda:*:211
_cvms:*:212
_usbmuxd:*:213
_postgres:*:216
_devicemgr:*:220
_webauthserver:*:221
_netbios:*:222
_pcastfeedadmins:*:223
_warmd:*:224
_pcastlibrary:*:225
_podcastlibraryfeedadmins:*:226
_dovenull:*:227
_netstatistics:*:228
同时,getgrgid函数可以指定gid得到组信息。


Q: 获取用户信息呢?

A: 可以使用getuid或者geteuid得到需要的用户ID。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>
#include <unistd.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_UD(intValue)	printf(#intValue" is %u\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

int main()
{
    PRINT_UD(getuid())
    PRINT_UD(geteuid())
    return 0;
}

运行结果(依赖于系统):

笔者当时系统的用户为xichen, 使用id  xichen得到此用户的信息:

可以看到uid确实是501.至于uid和euid的区别,这里就不做分析了。同理,如果要获取组id, 可以使用getgid和getegid函数。


Q: 需要获取密码信息,如何得到?

A: 可以使用getpwent来获得。

struct passwd *getpwent(void);
它的使用类似getgrent函数。


Q: 关于文件操作,creat函数和open函数有什么区别?

A: 注意,它不是create,是creat.  creat相当于特殊的open函数,它和open的关系如下:

如下示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grp.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>

#define	PRINT_D(intValue)	printf(#intValue" is %d\n", (intValue));
#define	PRINT_UD(intValue)	printf(#intValue" is %u\n", (intValue));
#define	PRINT_L(longValue)	printf(#longValue" is %ld\n", (longValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

int main()
{
    int ret = creat("test", S_IRUSR);
    if(ret < 0)
    {
        perror("creat file error");
        return -1;
    }
    return 0;
}
执行后,它会会得到一个空文件test.


Q: 关于文件操作,dup和dup2到底有什么作用?

A: 原型如下:

int  dup(int fildes);
int  dup2(int fildes, int fildes2);

dup可以复制给定的文件描述符,返回新的文件描述符,两个文件描述符共享相同的系统内部数据结构;

dup2可以将第一个参数指定的文件描述符的信息恢复到第二个参数指定的文件描述符,关于基本输入输出那篇文章最后已经使用了dup2进行恢复,这里不做介绍。

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));

int main()
{
    int fd = dup(fileno(stdout));
    FILE *file;
    PRINT_D(fd)
    
    // use stdout to output
    fprintf(stdout, "uses fd:%d output\n", fileno(stdout));
    
    // get the FILE * by file descriptor
    file = fdopen(fd, "w");
    if(!file)
    {
        perror("fdopen error");
        close(fd);
        return -1;
    }
    
    // use new fd to output
    fprintf(file, "uses fd:%d output\n", fd);
    
    fclose(file);
    return 0;
}

可以看到,dup产生的新文件描述符值为3,也可以和stdout(文件描述符对应于1)一样进行标准输出。


Q: fflush和fsync到底有什么区别?

A: fflush只是将数据写到内核缓冲区,并不一定写到设备上;fsync会将内核缓冲区中需要被写到设备上的数据写到设备上;


Q: 有的时候需要生成一个临时文件,自己去创建可能重复,可能覆盖已有的文件,想要生成一个随机的文件,有相关函数么?

A: 是的。mkstemp函数可以完成此功能。

int mkstemp(char *template);

示例代码:

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

#define ERR_PERROR_RETURN(str) \
        if(ret < 0)     \
        {       \
            perror(#str" error");   \
            return -1;              \
        }   

int main()
{
    // mkstemp's first argument should has postfix XXXXXX, or the result will be not what you need.
    char buf[] = "hello-XXXXXX";
    int ret = mkstemp(buf);
    ERR_PERROR_RETURN(mkstemp)
    PRINT_STR(buf)
    
    return 0;
}

运行结果:

用ls -l命令查看是否创建了上面的文件:

可以看到,确实创建了这样的文件。


Q: 文件操作函数clearerr函数该在何时使用?

A: 当操作文件后出现了错误但是又需要继续操作时可以用clearerr清除错误标志位,以避免后面的文件操作因为ferror的判断而导致错误。

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

#define ERR_PERROR_RETURN(str) \
        if(ret < 0)     \
        {       \
            perror(#str" error");   \
            return -1;              \
        }   

int main()
{
    FILE *fp = fopen("test", "r");
    if(fp)
    {
        int ch = 'a';
        fputc(ch, fp);
        if(ferror(fp))
        {
            printf("ferror...\n");
        }
        ch = fgetc(fp);
        printf("%c\n", ch);
        PRINT_D(ferror(fp))
        
        fclose(fp);
    }
    
    return 0;
}

运行:

可以看到出现ferror后,没有清除错误标志位,后面判断ferror依然为错误。所以,可以增加clearerr函数。

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str));

#define ERR_PERROR_RETURN(str) \
        if(ret < 0)     \
        {       \
            perror(#str" error");   \
            return -1;              \
        }   

int main()
{
    FILE *fp = fopen("test", "r");
    if(fp)
    {
        int ch = 'a';
        fputc(ch, fp);
        if(ferror(fp))
        {
            printf("ferror...\n");
            clearerr(fp);
        }
        ch = fgetc(fp);
        printf("%c\n", ch);
        PRINT_D(ferror(fp))
        
        fclose(fp);
    }
    
    return 0;
}
运行:


发现最后ferror判断已经正常了。


Q: 关于文件缓冲区操作,有setbuf, setbuffer, setvbuf, setlinebuf, 它们究竟什么关系?

A: 现依依介绍。下面先举个关于setvbuf的例子:

int  setvbuf(FILE *restrict stream, char *restrict buf, int type, size_t size);
第一个参数为文件指针;第二个参数为buf地址;第三个参数为缓冲区类型,有无缓冲,行缓冲和全缓冲3中方式;第四个参数为缓冲区大小。

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 

#define PAUSE       { getc(stdin);    rewind(stdin); }

static  char    buf[BUFSIZ];

int main()
{
    setvbuf(stdout, buf, _IOFBF, 6);
    printf("hello");
    PAUSE
    
    return 0;
}
如上,设置标准输出的缓冲区为buf, 且大小为6, 采用全缓冲;所以后面执行的时候,输出hello,它是5个字节,没有达到缓冲区大小6,所以不会立即输出;执行到PAUSE,任意输入一个字符,hello会被输出。

执行后,随意输入一个字符,比如上面的a,然后换行,hello才被显示。

如果将代码setvbuf最后的参数改为5,那么hello会在程序运行后便输出。

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 

#define PAUSE       { getc(stdin);    rewind(stdin); }

static  char    buf[BUFSIZ];

int main()
{
    setvbuf(stdout, buf, _IOFBF, 5);
    printf("hello");
    PAUSE
    
    return 0;
}
运行:


接着介绍setbuf函数:

void  setbuf(FILE *restrict stream, char *restrict buf);
它实际上就等同于如下代码:

接下来是setlinebuf函数:

int  setlinebuf(FILE *stream);
它等同于:

不过它的缓冲区大小取决于调用者。

setbuffer函数:

void  setbuffer(FILE *stream, char *buf, int size);
类似的,下面为测试代码:

#include <stdio.h>
#include <unistd.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 

#define PAUSE       { getc(stdin);    rewind(stdin); }

static  char    buf[BUFSIZ];

int main()
{
    setbuffer(stdout, buf, 5);
    printf("hello");
    
    PAUSE
    
    return 0;
}
运行结果就不写了。


Q: 关于进程控制的函数exit和_exit到底有什么区别?

A: 区别在于_exit退出进程前不会刷新缓冲区、关闭流等操作,而exit函数会。如下代码(文件名为testForC.c):

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 

int main()
{
    printf("hello");
    exit(0);
}

运行:

如果使用_exit函数:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 

int main()
{
    printf("hello");
    _exit(0);
}
运行:

可以发现运行它并没有输出缓冲区中的hello.


Q: 如何获取进程的优先级?

A: 使用getpriority即可。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define PRINT_D(intValue)   printf(#intValue" is %d\n", (intValue));
#define PRINT_STR(str)      printf(#str" is %s\n", (str)); 
#define PAUSE       { getc(stdin);    rewind(stdin); }

int main()
{
    pid_t   pid = getpid();
    int     priority = getpriority(PRIO_PROCESS, pid);
    PRINT_D(priority)
    PAUSE
    
    return 0;
}

运行后:

使用如下命令得到它的优先级:ps -l

可以看到testForC进程的优先级列NI也为0.和上面得到的是一致的。

如果要修改优先级,可以使用setpriority函数或者nice函数。


xichen

2012-5-16 17:53:01


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值