zoukankan      html  css  js  c++  java
  • linux之chdir函数解析

    [lingyun@localhost chdir]$ ls
    chdir.c
    [lingyun@localhost chdir]$ cat chdir.c 
    /*********************************************************************************
     *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
     *                  All rights reserved.
     *
     *       Filename:  chdir.c
     *    Description:  This file 
     *                 
     *        Version:  1.0.0(08/06/2013~)
     *         Author:  fulinux <fulinux@sina.com>
     *      ChangeLog:  1, Release initial version on "08/06/2013 03:35:13 PM"
     *                 
     ********************************************************************************/


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


    int main(void)
    {
        if(chdir("/tmp") < 0)
        {
            perror("chdir");
            exit(1);
        }
        printf("chdir to /tmp successded ");
        exit(0);
    }
    [lingyun@localhost chdir]$ gcc chdir.c 
    [lingyun@localhost chdir]$ pwd
    /home/lingyun/apue/chdir
    [lingyun@localhost chdir]$ ./a.out 
    chdir to /tmp successded
    [lingyun@localhost chdir]$ pwd
    /home/lingyun/apue/chdir

    [lingyun@localhost chdir]$ 

    执行./a.out程序的shell的当前工作目录没有变,其原因是创建了一个子进程并由该子程执行./a.out程序。记得有一次我把文件系统整个都删了,什么命令都不能执行,但是cd命令可以,apue上说cd命令是直接包含在shell程序中的。

  • 相关阅读:
    [极客大挑战 2019]BuyFlag
    [极客大挑战 2019]BabySQL
    [网鼎杯 2018]Fakebook
    C语言学习笔记_内存数据和字符串
    剑指OFFER_数据流中的中位数
    剑指OFFER_滑动窗口的最大值
    剑指OFFER_矩阵中的路径
    C语言学习笔记_指针相关知识
    剑指OFFER_机器人的运动范围
    剑指OFFER_剪绳子
  • 原文地址:https://www.cnblogs.com/riskyer/p/3241637.html
Copyright © 2011-2022 走看看