杂记 (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 functionmain’:prod_cos.c:32:5: warning: implicit declaration of functionpthread_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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗换了主人不吃怎么办 遇到扔东西碰瓷怎么办 碰见碰瓷的人怎么办 开店遇上碰瓷的顾客怎么办 我刮到别人的车怎么办 新车被刮了漆怎么办 停车擦到别人车怎么办 骑自行车被汽车撞了怎么办 车停在小区被刮怎么办 机动车被自行车撞了怎么办 单车撞小车后被起诉怎么办 给小车撞到电动单车怎么办 车停在路边被自行车撞怎么办 撞了碰瓷的人怎么办 谷丙转氨酶46该怎么办 渣土车开飞机了怎么办 自己车撞自己车怎么办 撞了人没钱赔怎么办 闯红灯扣了6分怎么办 开共享汽车闯红灯了怎么办 新手如果不小心闯红灯怎么办 红绿灯左转车道直行了怎么办 跟着大车后面闯了红灯怎么办 宝宝私处好红怎么办呢 甲亢难怀孕怎么办才好 怀孕8周查出甲亢怎么办 电动车被交警拖走了怎么办 电动车车被城管拖走了怎么办 12123地理反编码失败怎么办 苹果手机地理反编码失败怎么办 城管执法过程被打怎么办 老婆看不起老公不让碰怎么办 老婆总不让碰该怎么办 机动车扣满12分怎么办 吊车吊运货物失控应该怎么办 车辆违章扣6分怎么办 最新交通法扣满12分怎么办 违章停车单丢了怎么办 违停告知单掉了怎么办 违章停车扣3分怎么办 驾驶证被扣12分怎么办