sipp代码 中 时间和多线程编程
来源:互联网 发布:求生之路 mac 编辑:程序博客网 时间:2024/06/01 18:09
sipp中多线程
代码拿出来:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/wait.h>int main(){ pid_t l_pid; char x[100]= "ls"; switch(l_pid = fork()) { case -1: // error when forking ! printf("Forking error main"); break; case 0: // first child process - execute the command if((l_pid = fork()) < 0) { printf("Forking error child"); } else { if( l_pid == 0){ int ret; ret = system("./time"); // second child runs if(ret == -1) { printf("system call error for %s",x); } } exit(-1); } break; default: // parent process continue // reap first child immediately pid_t ret; while ((ret=waitpid(l_pid, NULL, 0)) != l_pid) { if (ret != -1) { printf("waitpid returns %1d for child %1d",ret,l_pid); } } break; }}
两次 fork实现了孙子进程与父进程的脱离。
其中time的代码是:
#include <string.h>#include <unistd.h>#include <stdio.h>#include <time.h>using namespace std;char* formatTime (struct timeval* P_tv, bool microseconds){ static char L_time [50]; struct tm * L_currentDate; // Get the current date and time time(&P_tv->tv_sec); L_currentDate = localtime ((const time_t *)&P_tv->tv_sec); // Format the time if (L_currentDate == NULL) { memset (L_time, 0, 50); } else { if (microseconds) { sprintf(L_time, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d:%03.03f", L_currentDate->tm_year + 1900, L_currentDate->tm_mon + 1, L_currentDate->tm_mday, L_currentDate->tm_hour, L_currentDate->tm_min, L_currentDate->tm_sec, (double)P_tv->tv_usec/(double)1000.0); } else { sprintf(L_time, "%4.4d-%2.2d-%2.2d/t%2.2d:%2.2d:%2.2d:%3.3d/t%10.10d.%6.6d", L_currentDate->tm_year + 1900, L_currentDate->tm_mon + 1, L_currentDate->tm_mday, L_currentDate->tm_hour, L_currentDate->tm_min, L_currentDate->tm_sec, (int) (P_tv->tv_usec)/1000, (long) (P_tv->tv_sec), (long) (P_tv->tv_usec)); } } return (L_time);} /* end of formatTime */int main(){ struct timeval currenttime; printf("%s\n",formatTime(¤ttime,true)); sleep(5); printf("%s\n",formatTime(¤ttime,true));
waitpid等待子进程结束。
解释 如下:
wait() and waitpid()
The wait() system call suspends execution of the calling process until one of its children terminates. The call wait(&status) is
equivalent to:
waitpid(-1, &status, 0);
The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By
default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described
below.
The value of pid can be:
< -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid.
-1 meaning wait for any child process.
0 meaning wait for any child process whose process group ID is equal to that of the calling process.
> 0 meaning wait for the child whose process ID is equal to the value of pid.
而 localtime是获取当前时间
localtime:根据给定的与1970年1月1日相减得秒数,取得当地的时区的时间和日期
time:此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数
- sipp代码 中 时间和多线程编程
- SIPP 代码学习笔记2- 关于时间
- sipp
- SIPp
- SIPP
- SIPp
- 多线程编程中sleep和WaitForSingleObject
- C#中关于多线程编程的示例代码
- SIPP 代码学习笔记1- compress
- SIPP 代码学习笔记3- 关于函数
- 《代码的未来》 第一章:编程的时间和空间
- VC中多线程编程
- VC中多线程编程
- VC中多线程编程
- C# 中多线程编程
- C#中多线程编程
- iOS中多线程编程
- python中多线程编程
- java获取年份的第一天和最后一天
- Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
- jQuery技巧总结
- Eclipse 常用快捷键
- 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
- sipp代码 中 时间和多线程编程
- C语言开发中的内存检测基本模块
- QGLWidget类函数说明及图形保存
- Android.mk 简介
- java转义url中文字符
- sqlite文件大小问题 VACUUM
- 金蝶软件快捷键大全
- Spring框架快速入门之简介
- Fedora环境下Gtk+开发环境配置