杂记 (3) —— C and linux
来源:互联网 发布:php linux 删除文件夹 编辑:程序博客网 时间:2024/05/21 03:28
问题
github, C pthread, C pointer
- 遇到一个奇怪的现象. 在移动硬盘中自己编译c程序得到的可执行文件没有可执行属性,使用root +x也不能改变他的属性. 当我把它放入自己电脑中的硬盘后,就可以chmod +x了.
自己猜想这可能和文件系统相关.
fdisk -lDevice Boot Start End Sectors Size Id Type/dev/sda1 * 2048 167776255 167774208 80G 7 HPFS/NTFS/exFAT/dev/sda2 167778302 976773119 808994818 385.8G f W95 Ext'd (LBA)/dev/sda5 167778304 323217075 155438772 74.1G 7 HPFS/NTFS/exFAT/dev/sda6 438315008 708849663 270534656 129G 7 HPFS/NTFS/exFAT/dev/sda7 708851712 976773119 267921408 127.8G 7 HPFS/NTFS/exFAT/dev/sda8 430364672 438300671 7936000 3.8G 82 Linux swap / Solaris/dev/sda9 323217408 430348287 107130880 51.1G 83 LinuxPartition 2 does not start on physical sector boundary.Partition table entries are not in disk order.Device Boot Start End Sectors Size Id Type/dev/sdb1 * 256 976707583 976707328 465.7G c W95 FAT32 (LBA)
- 查看自己的github ID
https://api.github.com/users/自己的用户名 - /dev/stdin, /dev/stdout, /dev/stderr分别对应着/dev/fd/0, /dev/fd/1, /dev/fd/2
- 编译出错:
warning: implicit declaration of function ‘pthread_setconcurrency’
gcc -o prod_cos prod_cos.c -pthreadprod_cos.c: In function ‘main’:prod_cos.c:32:5: warning: implicit declaration of function ‘pthread_setconcurrency’ [-Wimplicit-function-declaration] pthread_setconcurrency(threads);
查看/usr/include/pthread.h
458 #ifdef __USE_UNIX98 459 /* Determine level of concurrency. */ 460 extern int pthread_getconcurrency (void) __THROW; 461 462 /* Set new concurrency level to LEVEL. */ 463 extern int pthread_setconcurrency (int __level) __THROW; 464 #endif
所以我们需要自己定义__USE_UNIX98,具体可以这样:
在当前目录下写一个unix98.h, 在unix98.h中写入#define __USE_UNIX98
然后在#include <pthread>
前加上 #include "unix98.h"
获取线程的ID号
499 #define __NR_gettid 178
500 __SYSCALL(__NR_gettid, sys_gettid)怎样使用gdb()来看线程的栈空间
运行相关的程序:./main
查看其pid:ps aux|grep main
运行gdb:
gdb attach pidthread apply all bt
- 用指针给数组填充0
#include <stdio.h>int main(){ int a[5]={1,2,3,4,5}; int *p=a; int dex=0; while((p+dex) <= &a[4]){ printf("%p\n",(p+dex)); *(p+dex) = 0; dex++; } p=NULL; int i; for(i=0;i<5;i++){ printf("%d ",a[i]); } puts(""); return 0;}
./t0x7fff100d68100x7fff100d68140x7fff100d68180x7fff100d681c0x7fff100d68200 0 0 0 0
- 怎样给多维指针数组赋值NULL?
一个例子:
#include <stdio.h>#include <stdlib.h>int main(){ int *p[4][4][4] = {{{NULL}}}; int *a = NULL; printf("the null pointer is %p.\n",a); int i,j,k; for(i=0;i<4;i++){ for(j=0;j<4;j++){ for(k=0;k<4;k++){ printf("%p ",p[i][j][k]); } printf("\n"); } puts(""); } return 0;}
运行:
./t3the null pointer is (nil).(nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil) (nil)
- 多次free释放指针会怎么样?
#include <stdio.h>#include <stdlib.h>int main(){ int *p =(int *)malloc(sizeof(int)); *p=12; printf("number is %d , this pointer is %p.\n",*p,p); free(p); printf("number is %d , this pointer is %p.\n",*p,p); return 0;}
./t4number is 12 , this pointer is 0x1b95010.number is 0 , this pointer is 0x1b95010.
别以为这样就是安全的,在工程中他可能会导致系统产生的这样的错误信息: signal SIGABRT (多次释放内存)
记录
unix环境编程
- 信号SIGSEGV
某一个线程执行了无效指针访问 - 不能被捕获的信号
SIGKILL, SIGSTOP - sleep_us()
睡眠单位是微秒。 - system()的工作原理及其妙用 (摘自百度百科)
system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
system(“pause”)可以实现冻结屏幕,便于观察程序的执行结果;
system(“CLS”)可以实现清屏操作;
system(“color 3A”) 调用color函数可以改变控制台的前景色和背景,其中color后面的3是背景色代号,A是前景色代号
自己的vimrc:
set numberset backspace=2set hlsearchset rulerset nuset bg=darkset tabstop=4set noexpandtabset softtabstop=4set shiftwidth=4set cindentsyntax on
关于多行缩进:
在visual模式 - visual block模式下选中多行,shift + ‘>’就能缩进。
命令
set noexpandtabset softtabstop=4
和makefile的编写有关系。
2 0
- 杂记 (3) —— C and linux
- 杂记 (2) —— linux and C
- 杂记 (4) —— linux and coding
- linux C线程杂记
- linux C杂记(一)
- linux c 一站式学习 杂记
- C语言学习——数据类型杂记
- 嵌入式C语言基础—杂记一
- linux杂记3
- [杂记]C/C++ ·书籍·And More
- C杂记
- linux杂记
- Linux杂记
- linux 杂记
- linux 杂记
- Linux 杂记
- linux杂记
- linux杂记
- 树莓派之网络设置
- 字符串反转
- Struts2自学入门(五)——OGNL
- Hdu 3709 Balanced Number
- 单例模式
- 杂记 (3) —— C and linux
- Codeforces 50A(往矩形里填多米诺骨牌)
- 商务旅行_codevs1036_lca
- bzoj 2982: combination lucas定理+乘法逆元
- SPI通信总线-51
- java 集合小记1
- Executor并发性能对比
- 一个Hello World的Arm汇编指令
- Less中文网 教程 概览部分