C指针原理(88)-LINUX应用(1)
来源:互联网 发布:淘宝店铺标志300x300 编辑:程序博客网 时间:2024/05/29 03:24
一、在linux平台下,每个线程可有专用数据:
#include <pthread.h>
#include <stdio.h>struct mydata{
int x;
char c[4];
};
pthread_t pthreada,pthreadb;
pthread_key_t datakey;//每个进程创建一次,不同的线程,同样名字的键指向不同的地方
void *cleanup_mydata(void *dataptr){//删除键时调用的
free((struct mydata*)dataptr);
}
void anum1(){
int rc;
struct mydata *mdata=(struct mydata*)malloc(sizeof(struct mydata));
mdata->x=1;
mdata->c[0]='a';
mdata->c[1]='\0';
rc=pthread_setspecific(datakey,(void*)mdata);//设置键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
sleep(1);
struct mydata *mmdata=(struct mydata*)pthread_getspecific(datakey);//取出键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
printf("-%d-%s\n",mmdata->x,mmdata->c);
fflush(stdout);
}
void bnum2(){
int rc;
struct mydata *mdata=(struct mydata*)malloc(sizeof(struct mydata));
mdata->x=2;
mdata->c[0]='b';
mdata->c[1]='\0';
rc=pthread_setspecific(datakey,(void*)mdata);//设置键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
sleep(1);
struct mydata *mmdata=(struct mydata*)pthread_getspecific(datakey);//取出键指向的值,注意这个mdata为值的内存,必须使用指针的方式指向内存
printf("-%d-%s\n",mmdata->x,mmdata->c);
fflush(stdout);
}
int main(void){
int rc;
rc=pthread_key_create(&datakey,cleanup_mydata);//为键删除时的清理函数
pthread_create(&pthreada,NULL,anum1,NULL);
pthread_create(&pthreadb,NULL,bnum2,NULL);
sleep(3);
pthread_join(pthreada,NULL);
pthread_join(pthreadb,NULL);
rc=pthread_key_delete(datakey); //仅删除键,但不删除值指向的内存,线程终止调用用户自定义的删除函数,本例中为cleanup_mydata
}
二、摸拟shell
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
通过execlp函数来实现 ,execlp函数用于执行文件
其参数与说明为:
#include <process.h>int execlp( const char * file, const char * arg0, const char * arg1, … const char * argn, NULL );
Arguments:
waitpid()
wait for process termination
Function
SYNOPSIS DESCRIPTION PARAMETERS RETURN VALUES CONFORMANCE MULTITHREAD SAFETY LEVEL PORTING ISSUES AVAILABILITY SEE ALSOSYNOPSIS
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *stat_loc, int options);
DESCRIPTION
The waitpid() function lets the calling process obtain status information about one of its child processes. If status information is available for two or more child processes, the order in which their status is reported is unspecified. If more than one thread is suspended in waitpid() awaiting termination of the same process, exactly one thread returns the process status at the time of the target child process termination. The other threads return -1, with errno set to ECHILD.
If the calling process sets SIGCHLD to SIG_IGN, and the process has no unwaited for children that were transformed into zombie processes, the calling thread blocks until all of the children of the process terminate, at which time waitpid() returns -1 with errno set to ECHILD.
If the parent process terminates without waiting for all of its child processes to terminate, the remaining child processes are assigned a new parent process ID corresponding to a system-level process.
PARAMETERS
Specifies a set of child processes for which the status is requested:
- If pid is equal to -1, status is requested for any child process. In this respect, waitpid() is equivalent to wait().
- If pid is greater than 0, it specifies the process ID of a single child process for which status is requested.
- If pid is 0, status is requested for any child process whose process group ID is equal to that of the calling process. This setting is not currently supported.
- If pid is less than -1, status is requested for any child process whose process group ID is equal to the absolute value of pid. This setting is not currently supported.
Specifies the location to which the child process' exit status is stored. If NULL is passed, no exit status is returned. Otherwise, the following macros defined in <sys/wait.h> can be used to evaluate the returned status:
Evaluates to a non-zero value if status was returned for a child process that exited normally.
If the value of WIFEXITED(s) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to exit() or _exit(), or to the value that the child process returned from main().
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught.
If the value of WIFSIGNALED(s) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught, and whose default action is to dump core.
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught, and whose default action is to dump core.
If the value of WIFCORED(s) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
Evaluates to a non-zero value if status was returned for a child process that is currently stopped.
If the value of WIFSTOPPED(s) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.
Is the bitwise inclusive-OR of zero or more of the following flags, defined in <sys/wait.h>:
The waitpid() function does not suspend execution of the calling thread if status is not immediately available for one of the child processes specified by pid.
The status of any child processes specified by pid that are stopped, and whose status has not yet been reported since they stopped, is also reported to the requesting thread. This value is currently not supported, and is ignored.
RETURN VALUES
If waitpid() was invoked with WNOHANG set in options, and there are children specified by pid for which status is not available, waitpid() returns 0. If WNOHANG was not set,waitpid() returns the process ID of a child when the status of that child is available. Otherwise, it returns -1 and sets errno to one of the following values:
The process or process group specified by pid does not exist or is not a child of the calling process.
stat_loc is not a writable address.
The function was interrupted by a signal. The value of the location pointed to by stat_loc is undefined.
The options argument is not valid.
pid specifies a process group (0 or less than -1), which is not currently supported.
#include <stdio.h>
#include <sys/wait.h>
#define MAXLINE 100
//execlp模拟SHELL
int main(void){
int pid;
int jg,status,len;
char buf[MAXLINE];
printf("\n##myhaspl~~");//自定义的shell提示符
while(fgets(buf,MAXLINE,stdin)!=NULL){//读入一行
len=strlen(buf)-1;
if (buf[len]=='\n'){ //去除换行符,execlp只接受以NULL结尾
buf[len]=0;
}
pid=fork();
if (pid<0){
printf("fork error!\n");
}
else if (pid==0){//子进程
printf("\n");
if (buf[0]=='Q'&&strlen(buf)==1){//键入Q表示退出shell
exit(200);
}
jg=execlp(buf,buf,(char *)0);
if (jg==-1){//错误
printf("不能执行:%s\n",buf);
exit(127);
}
exit(0);
}
if ((jg==waitpid(pid,&status,0))<0){//父进程
printf("waitpid error\n");
}
if (WEXITSTATUS(status)==200) {//WEXITSTATUS计算返回值
printf("退出....\n");
break;
}
printf("\n##myhaspl~~");//自定义的shell提示符
}
exit(0);
}
执行:
deepfuture@deepfuture-laptop:~/private/mytest$ gcc -o test21 test21.c
test21.c: In function ‘main’:
test21.c:15: warning: incompatible implicit declaration of built-in function ‘strlen’
test21.c:26: warning: incompatible implicit declaration of built-in function ‘exit’
test21.c:28: warning: incompatible implicit declaration of built-in function ‘execlp’
test21.c:31: warning: incompatible implicit declaration of built-in function ‘exit’
test21.c:33: warning: incompatible implicit declaration of built-in function ‘exit’
test21.c:44: warning: incompatible implicit declaration of built-in function ‘exit’
deepfuture@deepfuture-laptop:~/private/mytest$ ./test21
##myhaspl~~ls
1 pvmtest test20 testbswap testmul.s x
ex.txt test test20.c testbswap.s testmutex x.c
gmon.out test12.s test21 test.c testmutex.c xx
hello test13 test21.c test.c~ testpopen xx.c
hello.c test13.c test2.c testmes testpopen.c xx.txt
hello.o test15 test6 testmes.c testpvm1.c xxx.txt
hello.s test15.c test66 testmesrecv testpvm2.c xxxx.txt
main test19 test66.s testmesrecv.c testpx xy
main.c test19.c test6.c testmessnd testpx1
main.c~ test1.c testasmc testmessnd.c testpx1.s
myhello.txt test2 testasmc.c testmul testpx.s
##myhaspl~~xx
不能执行:xx
##myhaspl~~Q
退出....
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
- C指针原理(88)-LINUX应用(1)
- C指针原理(89)-LINUX应用(2)-线程池
- C指针原理(92)-C应用技巧(1)
- C指针原理(1)
- C指针原理(90)-LINUX应用(4)-带超时处理的tcp服务
- C指针原理(91)-LINUX应用(3)-进程写文件,另一进程读输入
- C指针原理(93)-C应用技巧(2)
- C指针原理(93)-C应用技巧(3)
- C指针原理(96)-C基础综合应用
- C指针原理(1)-AT&T汇编
- C指针原理(77)-递归(1)
- C指针原理(94)-汇编基础(1)
- C指针原理(2)
- C指针原理(3)
- C指针原理(4)
- C指针原理(5)
- C指针原理(6)
- C指针原理(8)
- Drools 规则文件 ——语法属性
- ArcEngine报0x80040228的错误
- 利用ADS1.2和JLINK V8搭建ARM7开发平台
- 代做单片机毕业设计 proteus仿真 课程设计
- usaco 1.3 Calf Flac(暴搜)
- C指针原理(88)-LINUX应用(1)
- 网曝老师体罚学生后微博喊爽 学校:无体罚
- HTML5 Form Elements
- 你想让电脑随机输出英语字母表的任意数吗?
- vm virtualbox 按照centos无法上网问题解决
- 坐标转换
- 报文:分组、帧、数据报和信元
- 修改/etc/environment 导致开机不能登录的解决办法
- BCB使用TImage做探地雷达图形动态显示