我想通过子进程中的结构将测量数据发送到Parent。父进程必须打印10个测量数据的平均值。
funcs.h中的结构数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
namespace ConvertListToDataTable
{
public static class Program
{
public static void Main(string[] args)
{
List<MyObject> list = new List<MyObject>();
for (int i = 0; i < 5; i++)
{
list.Add(new MyObject { Sno = i, Name = i.ToString() + "-KarthiK", Dat = DateTime.Now.AddSeconds(i) });
}
DataTable dt = ConvertListToDataTable(list);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine();
for (int x = 0; x < dt.Columns.Count; x++)
{
Console.Write(row[x].ToString() + " ");
}
}
Console.ReadLine();
}
public class MyObject
{
public int Sno { get; set; }
public string Name { get; set; }
public DateTime Dat { get; set; }
}
public static DataTable ConvertListToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor propertyDescriptor = props[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dataTable.Columns.Add(propertyDescriptor.Name, type);
}
object[] values = new object[props.Count];
foreach (T iListItem in iList)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
}
uebung2_1.c(主要):
struct data{
int nr;
float temp;
float bst;
float abst;
};
子进程通过linux中断每500ns发送一次测量数据。我首先使用char数组测试它并且它成功了。
sohn.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include "funcs.h"
#define READ_END 0
#define WRITE_END 1
#define CHILD "./sohn"
int main(){
int w_status, pid_fork, i, k;
double midT, midB, midA;
int pfd[2];
struct data buf;
if(pipe(pfd) == -1){
perror("ERROR creating a pipe\n");
exit(EXIT_FAILURE);
}
pid_fork = fork();
switch(pid_fork){
case(-1):
perror("fork()");
return EXIT_FAILURE;
case(0):
dup2(pfd[WRITE_END], STDOUT_FILENO);
close(pfd[READ_END]);
execlp(CHILD,CHILD,NULL);
perror("exec is failed");
break;
default:
close(pfd[WRITE_END]);
printf("Sohn gestartet mit PID %d\n", pid_fork);
midT = midB = midA = 0;
i = k = 0;
while(1){
read(pfd[READ_END],&buf,sizeof(struct data));
midT += buf.temp;
midB += buf.bst;
midA += buf.abst;
i++;
if(i == 10){
midT /=10;
midB /=10;
midA /=10;
printf("Messung. %4i: Temp.: %4.1lf, Beleucht.: %4lf, Abstand: %3.1lf\n",k++, midT, midB, midA);
midT = midB = midA = 0;
i = 0;
}
}
while(i != -1){
i = wait(&w_status);
if(i != -1)
printf("\nSohn beendet. Exit Status: %x Nummer des Sohn-Prozesses %d",
(w_status>>8) & 0xff, i);
else
printf("\nLetzter Sohn beendet!\n");
}
}
return 0;
}
输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#include "funcs.h"
#define INTERVAL 500000
int howmany = 0;
void alarm_wakeup (int i)
{
struct itimerval tout_val;
howmany += INTERVAL;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = 0;
tout_val.it_value.tv_usec = INTERVAL;
setitimer(ITIMER_REAL, &tout_val,0);
}
void exit_func (int i)
{
signal(SIGINT,exit_func);
exit(0);
}
int main(){
struct data mess;
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = 0;
tout_val.it_value.tv_usec = INTERVAL;
setitimer(ITIMER_REAL, &tout_val,0);
signal(SIGALRM,alarm_wakeup);
signal(SIGINT,exit_func);
srand(time(0));
mess.nr = 0;
while (1) {
mess.nr++;
mess.temp = (rand()%1001)/10 - 20;
mess.bst = rand()%1000 + 1;
mess.abst = rand()%26 + 4;
write(STDIN_FILENO,&mess, sizeof(struct data));
sleep(10);
}
exit(0);
}
使用strace输出:
Sohn gestartet mit PID 3488
�BhB�A@��@�A�B�!DA@ATCpA�B@D A4B�C�A0A@#D0dB�sDA 4B��CpA
��\D�A^C
我用ctrl + c杀了进程。为什么是象形文字?
-------------------------------------------- -------------------------------
更新 我在孩子(sohn.c)写的标准输入,但它对输出很好。感谢Joachim Pileborg