Chinaunix首页 | 论坛 | 博客
  • 博客访问: 196820
  • 博文数量: 71
  • 博客积分: 3135
  • 博客等级: 中校
  • 技术积分: 740
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-23 13:29
文章分类

全部博文(71)

文章存档

2012年(3)

2011年(1)

2010年(7)

2009年(3)

2008年(15)

2007年(24)

2006年(18)

我的朋友

分类: LINUX

2007-12-12 11:49:20

前面的几篇文章已经介绍了嵌入式平台的搭建,余下的就是开发了。开发工具为vim或者windows下你喜欢用的编辑器。vim的配置已经完成,功能超过了很多ide。
一,vim的使用:
    用putty登录到linux主机,
   

@debian~$vim test.c


    F12键: buffer文件查看
    wm: 文件浏览
    ctrl+]: 代码跳转,基于用ctags命令生成了源文件的tags标签,并把路径加进.vimrc配置文件里面
    ctrl+T: 代码回跳
    ctrl+j[k][l]窗口跳转
    :make  编译指令,前提是写好了Makefile文件
    :cw   切分窗口,比如编译时的报警或者错误
二,开发用到的知识点:
    1,fork()函数
       说明:fork函数调用一次返回两次,向父进程返回子进程ID,向子进程返回0。这是因为父进程可能有很多子进程,必须用pid来标识子进程,而进程只有一个父进程,用getppid()获得。子进程是原进程的拷贝。
    2.pid_t waitpid(pid_t pid,int * status,int options)函数
       说明:waitpid会暂时停止目前进程的执行,直到子进程结束或者有信号来到。参数pid为要等待执行结束的进程id(如子进程)。
    例子:

#include "../include/apue.h"
#include <sys/wait.h>

int main()
{
    char buf[MAXLINE];
    pid_t pid;
    int status;
    int i=0;

    printf("%%:"); //输出%:


    //从标准输入读数据
    while (fgets(buf, MAXLINE, stdin) != NULL)
    {
        if (buf[strlen(buf)-1] == '\n') //如果最后一个字符为换行符,那么把它置为0
        {
            buf[strlen(buf)-1] = 0; //这个东西主要是供给execlp调用,execlp要求最后一个字符为0
        }
        if ((pid = fork()) < 0)
        {
            err_sys("fork error");
        }
        else if (pid == 0) //子进程

        {
            printf("In childprocess:\n");
            sleep(3); //停三秒
            execlp(buf, (char *)0); //执行命令,来自标准输入的buf
            err_ret("couldn't execute:%s", buf);
            exit(127);
        }
        else
        {
            if ((pid= waitpid(pid, &status,0)) < 0) //等待子进程结束后再执行下面的代码
            {
                err_sys("waitpid error");
            }
            printf("In parentprocess%d:\n", i++);    
        }
        printf("%%:");
    }
    exit(0);
}

执行结果:
%:date
In childprocess:
2007年 12月 13日 星期四 10:57:09 EST
In parentprocess0:
%:
注释掉waitpid后,执行结果为:
%:date
In childprocess:
In parentprocess0:
2007年 12月 13日 星期四 11:00:16 EST
由此可以看出,注释掉waitpid后,父进程没等子进程结束就先自己执行完后就结束了。而子进程休眠了3秒后才执行命令,所以执行结果输出到下方。
    3.pipe

/*
 * =====================================================================================
 *
 * Filename: pipedemo.c
 *
 * Description: pipe(建立管道)
 *
 * Version: 1.0
 * Created: 2007年12月19日 13时25分42秒
 * Revision: none
 * Compiler: gcc
 *
 * Author: jsli,shuangquansq@163.com
 * Company: hn-unis
 *
 * =====================================================================================
 */


/*
 *头文件 #include
 *函数调用 int pipe(int filedes[2])
 --------------------------------
 *函数说明:
 *pipe()会建立管道,并将文件描述词由参数filedes数组返回
 *filedes[0]为管道里的读取端
 *filedes[1]则为管道的写入端
 *-------------------------------
 * 返回值:
 *若成功则返回零,否则返回-1,错误原因存于errno中
 *-------------------------------
 *错误代码:
 *EMFILE 进程已用完文件描述词最大量
 *ENFILE 系统已无文件描述词可用
 *EFAULT 参数filedes数组地址不合法
 *
 */

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>

int main()
{
    int filedes[2];
    char buf[80];
    pid_t pid;
    int status;

    pipe(filedes);

    /*
     *调用fork函数,父进程写数据到管道里
     *子进程从管道里读数据
     */


    if ((pid = fork()>0))
    {
        printf("In the parent process:\n");
        char str[] = "test pipe data!";
        write(filedes[1], str, sizeof(str));

    }

    else
    {
    //    sleep(3);

        printf("In child process:\n");
        read(filedes[0], buf,sizeof(buf));
        printf("child process print:\n %s", buf);

    }
    
    waitpid(pid, NULL, 0);
    return 0;
}

阅读(814) | 评论(0) | 转发(0) |
0

上一篇:堕落街 爱之过程

下一篇:冬至心冷

给主人留下些什么吧!~~