gg
时间:2019-09-14
本文章向大家介绍gg,主要包括gg使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
编程题
3、12
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if(pid < 0){
printf("Process creation failed");
}
else if(pid == 0){
printf("Parent process pid:%d\n", getpid());
}
else{
printf("Parenet proces, my process is id:%d child process's id: %d\n", getpid(), pid);
sleep(1000); //Ensure that the child process exits before the parent process
}
return 0;
}
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
int main(int argc, char **argv)
{
if (argc > 2){
fprintf(stderr,"Args passed invaild!\n");
return 1;
}
pid_t pid;
pid = fork();
if (pid < 0){
fprintf(stderr,"Fork Failed\n");
return 1;
}
else if (pid == 0){
//child process
int num = atoi(argv[1]); //Convert the string to number
printf("%d,",num);
while (num != 1){
if (num % 2 == 0){
num = num / 2;
if (num == 1) printf("%d\n",num);
else printf("%d,",num);
}
else{
num = 3*num + 1;
printf("%d,",num);
}
}
}
else{
//Parent process
wait(NULL); //Wait until the child process completed
printf("Child Complete\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h> // read the number of the last error
#include <fcntl.h> // file descriptor CONSTANT mode values
int main (int argc, char *argv[]) {
/*
* File descriptor array.
* When we create our pipe, we give pipe() this array and it will
* put the reading[0] and writing[1] file descriptors for the pipe into it.
*/
int pipeFds[2];
int fileBytesLength;
char buffer[100];
char childBuffer[100];
// Check if 3 arguments were supplied.
if (argc != 3) {
perror("Filecopy: filecopy.exe [target] [destination]. \n");
exit(1);
}
char* srcFile = argv[1];
char* dstFile = argv[2];
// Attempt to create a pipe.
if (pipe(pipeFds) < 0) {
printf("Something went wrong creating the pipe! %s\n", strerror(errno));
exit(1);
}
// Fork child process
switch(fork()) {
// If there was an errorforking a child process
case -1:
printf("Error forking child process. %s\n", strerror(errno));
exit(1);
// If the current executing process is a child process
// Read the file from upstream parent process and write it to a new file.
case 0:
close(pipeFds[1]); // Close writing end of pipe upstream.
ssize_t num_bytes_child = read(pipeFds[0], childBuffer, sizeof(childBuffer)); // Read file contents from upstream pipe into childBuffer
close(pipeFds[0]); // close reading upstream pipe when we're done with it
int targetDesc = open(dstFile, O_CREAT | O_WRONLY); // Open a file for writing, create file descriptor.
write(targetDesc, childBuffer, num_bytes_child); // Write contents of buffer to new file descriptor.
// If the current process is the parent process.
// Read the file and send it down to the child process to write.
default:
close(pipeFds[0]); // close reading end of pipe downstream.
int fileInDesc = open(srcFile, O_RDONLY); // Read file into file descriptor
ssize_t num_bytes = read(fileInDesc, buffer, sizeof(buffer)); // Get number bytes to read
write(pipeFds[1], buffer, num_bytes); // Write bytes to child process.
close(pipeFds[1]); // Close writing downstream pipe when we're done with it.
}
return 0;
}
原文地址:https://www.cnblogs.com/lihello/p/11520495.html
- 认识ASP.NET MVC的5种AuthorizationFilter
- 2017十大“最差”密码出炉
- SVG图形绘制入门第一弹
- 使用自定义标记来构建页面
- ASP.NET MVC集成EntLib实现“自动化”异常处理[实例篇]
- ASP.NET Core管道深度剖析(2):创建一个“迷你版”的管道来模拟真实管道请求处理流程
- 机器学习实战之决策树
- 初学ReactJS,写了一个RadioButtonList组件
- 今天,你有微信小游戏提交审核吗?
- 云计算,迷你版线程同步
- Kubernetes 年度关键进展回顾
- 比较一下以“反射”和“表达式”执行方法的性能差异
- 人工智能芯片是什么?有什么用?
- 柯洁食言“复出”再战AI:明年4月见分晓
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Spring高级技术应用——百战商城实现(下)
- Go 字符串反转问题解决思路
- 剑指offer:go实现从尾到头打印链表
- 【go】剑指offer:3种方法寻找二进制1的个数
- 高并发技术
- Working with Errors in Go 1.13
- Hadoop技术(二)资源管理器YARN和分布式计算框架MapReduce
- CDH——Cloudera’s Distribution Including Apache Hadoop
- 【go】剑指offer:用26个英文字母组合表示列数
- Flink——运行在数据流上的有状态计算框架和处理引擎
- goland破解教程
- HTTP接口设计
- NSQ分布式消息基础入门
- 【golang】剑指offer 最大n位数
- Spark提交Jar任务警告: Initial job has not accepted any resources;