Linux C编程

0  define of function

gaofeng@ubuntu:$ grep " sleep ("   /usr/include/*.h
/usr/include/unistd.h:extern unsigned int sleep (unsigned int __seconds);

1、hello world

gaofeng@ubuntu:~/c_code$ cat test1.c
#include <stdio.h>
int main(int argc,char ** argv){
  printf("hello %d %s\n",argc,argv[0]);
  return 0;
}

2、c语言中static关键字用法详解

https://blog.csdn.net/keyeagle/article/details/6708077

https://blog.csdn.net/guotianqing/article/details/79828100

 

2、执行其它程序

execl, execvp

https://blog.csdn.net/u010006102/article/details/39960269

3、fork

gaofeng@ubuntu:~/c_code$ cat test3.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int glob = 10;
int main()
{
        int var = 100;
        pid_t pid = getpid();
        printf("before fork:\n");
        printf("pid=%d, glob=%d, var=%d\n",pid,glob,var);

        printf("after fork:\n");
        pid=fork();
        if( pid<0)
        {
                printf("error fork:%m\n");//错误码,系统全局变量
                exit(-1);
        }
        else if(pid==0)
        {
                glob++;
                var++;
        }
        else
        {
                sleep(2);
        }

        printf("pid = %d, glob = %d, var = %d\n",getpid(),glob,var);
        return 0;
}

gaofeng@ubuntu:~/c_code$ ./a.out
before fork:
pid=4593, glob=10, var=100
after fork:
pid = 4594, glob = 11, var = 101
pid = 4593, glob = 10, var = 100

4、Linux编程基础之进程等待(wait()函数) https://www.cnblogs.com/king-77024128/articles/2684317.html

linux c学习笔记----进程创建(fork,wait,waitpid) https://lobert.iteye.com/blog/1705164

fork与vfork的区别 https://blog.csdn.net/jianchi88/article/details/6985326

exit()与_exit()函数的区别https://blog.csdn.net/zz709196484/article/details/54770017

基于fork(),execvp()和wait()实现类linux下的bash——mybash

 

《Linux内核原理与分析》第九周作业

 

对"initscr"未定义的引用

因为 curses 不是linux里默认的C函数库,所以导致编译时未找到函数定义

需要 gcc Test1.c -l curses -o Test1
 

5、输入输出

       #include <stdio.h>

       int scanf(const char *format, ...);
       int fscanf(FILE *stream, const char *format, ...);
       int sscanf(const char *str, const char *format, ...);


       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
       int sprintf(char *str, const char *format, ...);
       int snprintf(char *str, size_t size, const char *format, ...);

6、打印当前函数,当前行,当前时间

#include <stdio.h>
void f(int x){printf("test %d\n",x); }
int main() { 
	  printf("test%s  %s  %s %s %d\n",__FILE__,__FUNCTION__,__DATE__,__TIME__,__LINE__);
	  f(__LINE__);
	return 0;
}
ANSI C标准中有几个标准预定义宏(也是常用的):
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;


7、

UNIX环境下的C 对二进制流文件的读写有两套班子:1) fopen,fread,fwrite ; 2) open, read, write

fopen是标准c里的,有缓冲,  而open是linux的系统调用,无缓冲.可以操作设备文件

对文件的读和写是最常用的文件操作。在C语言中提供了多种文件读写的函数:

·字符读写函数 :fgetc和fputc

·字符串读写函数:fgets和fputs

·数据块读写函数:fread和fwrite

·格式化读写函数:fscanf和fprinf 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值