我正在用C开发一个应用程序.父子进程通过管道进行通信.在写入管道之前,父进程执行另一个语句.在示例代码中,我使用sleep(10)来延迟.在子进程中,它应该从管道中读取数据.但是在子进程的管道读取端没有读取数据.
int main()
{
int pid;
FILE *fp;
fp = fopen("test.txt","w");
char *buff;
int fd[2];
int count = 0 ;
pipe(fd);
pid = fork();
if(pid == 0)
{
close(fd[1]);
ioctl(fd[0], FIONREAD, &count);
fprintf(fp,"Value of count: %d ",count);
buff = malloc(count);
fprintf(fp,"\n TIME before read: %s",__TIME__);
read(fd[0], buff, count);
fprintf(fp,"\nbuffer: %s\n TIME after read %s", buff, __TIME__);
}
else{
close(fd[0]);
sleep(10); //delay caused by application specific code replaced with sleep
write(fd[1],"THIS is it",10);
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使子进程等到数据写入另一端?
我正在尝试获得ppid我想要的过程。
我使用以下代码来获取pid
proc=subprocess.Popen('ps -ae | grep ruby', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()[0]
str = output.split()
Run Code Online (Sandbox Code Playgroud)
现在在str[0],我有进程的pid说ruby,我想获取ppid同一进程的父进程ID和子进程ID。
我需要在Solaris as well as Red Hat Enterprise Linux6.0上运行此解决方案
有什么办法可以得到喜欢getppid()和getchildid()吗?还是我需要grep再次通过命令进行拆分?
我使用sed命令将xml元素插入到现有的xml文件中.
我有xml文件
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
</Students>
Run Code Online (Sandbox Code Playgroud)
我想添加新元素作为
<student>
<name>NewName</name>
<id>NewID</id>
</student>
Run Code Online (Sandbox Code Playgroud)
所以我的新xml文件将是
<Students>
<student>
<name>john</>
<id>123</id>
</student>
<student>
<name>mike</name>
<id>234</id>
</student>
<student>
<name>NewName</name>
<id>NewID</id>
</student>
</Students>
Run Code Online (Sandbox Code Playgroud)
为此,我编写了shell脚本
#! /bin/bash
CONTENT="<student>
<name>NewName</name>
<id>NewID</id>
</student>"
#sed -i.bak '/<\/Students>/ i \ "$CONTENT" /root/1.xml
sed -i.bak '/<\/Students>/ i \'$CONTENT'/' /root/1.xml
Run Code Online (Sandbox Code Playgroud)
我收到错误了
sed: can't read <name>NewName</name>: No such file or directory
sed: can't read <id>NewID</id>: No such file or directory
sed: can't read </student>: No such file or directory …Run Code Online (Sandbox Code Playgroud) 我的数字为x,y,z和w.我试图以24小时格式创建最大可能时间.例:
我的方法是对所有数字进行排序.然后数小时检查小于等于2的数字,然后检查小时的下一个数字,检查小于等于4的数字,依此类推几分钟.(0-60分钟)
除了暴力解决方案,还有其他任何有效的方法吗?
我已经编写了以下代码来清楚地了解malloc和realloc.我使用malloc初始化指针然后使用realloc,我增加了数组的大小.但是当我运行代码时,我得到以下错误.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *p;
p = malloc(10);
p = " this is it";
printf("\n%s", p);
p = realloc(p, 14);
p[11] = 'A';
p[12] = 'B';
p[13] = 'C';
printf("\n %s", p) ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
ajay@ajay-K54L:~$ gcc te.c
ajay@ajay-K54L:~$ ./a.out
*** glibc detected *** ./a.out: realloc(): invalid pointer: 0x000000000040071c ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7fb111e88626]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x2de)[0x7fb111e8d3ee]
./a.out[0x4005dc]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fb111e2b76d]
./a.out[0x4004d9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 3027043 /home/ajay/a.out
00600000-00601000 r--p 00000000 08:03 3027043 /home/ajay/a.out
00601000-00602000 …Run Code Online (Sandbox Code Playgroud) 我正在我的代码中创建子进程。当我调用 fork() 时,子进程应该从下一条语句开始执行,但在我的代码中,子进程在 fork 调用之前执行语句。
#include<stdio.h>
int main()
{
int pid;
FILE *fp;
fp = fopen("oh.txt","w");
fprintf(fp,"i am before fork\n");
pid = fork();
if(pid == 0)
{
fprintf(fp,"i am inside child block\n");
}
else{
fprintf(fp,"i inside parent block\n");
}
fprintf(fp,"i am inside the common block to both parent and child\n");
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
i am before fork
i inside parent block
i am inside the common block to both parent and child
i am before fork
i …Run Code Online (Sandbox Code Playgroud) 我试图根据下拉框的值动态重定向页面.我在JavaScript中获得了下拉框的值.根据下拉值,我想重定向页面.
这是示例代码:
<script type="text/javascript">
function RedirectMe(){
var chosanDept = document.getElementById("Dept");
var str = chosanDept.options[chosanDept.selectedIndex].text;
if(str=='HR')
{
alert('Yes in IF' + str);
window.location = "http://www.google.com";
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这
chosanDept是获取下拉框值的变量.但我无法使用window.location,location.replace,location.href等各种函数重定向页面.还有一个我的if条件有效,我收到警报'在IF HR中是'
这里出了什么问题?
如何在 sklearn python 中重新训练我现有的机器学习模型?
我有数千条记录,我使用这些记录训练我的模型并.pkl使用pickle. 在第一次训练模型时,我warmStart = True在创建逻辑回归对象时使用了该参数。
示例代码:
log_regression_model = linear_model.LogisticRegression(warm_start = True)
log_regression_model.fit(X, Y)
# Saved this model as .pkl file on filesystem like pickle.dump(model,open('model.pkl', wb))
Run Code Online (Sandbox Code Playgroud)
我想让它与我每天都会获得的新数据保持同步。为此,我打开现有模型文件并获取过去 24 小时的新数据并再次训练。/
示例代码:
#open the model from filesystem
log_regression_model = pickle.load(open('model.pkl','rb'))
log_regression_model.fit(X, Y) # New X, Y here is data of last 24 hours only. Few hundreds records only.
Run Code Online (Sandbox Code Playgroud)
但是,当我通过从文件系统加载模型来重新训练模型时,它似乎删除了使用数千条记录创建的现有模型,并创建了过去 24 小时内包含数百条记录的新模型(具有数千条记录的模型大小为 3MB)在文件系统上,而新的重新训练模型只有 67KB)
我试过使用warmStart 选项。如何重新训练我的 LogisticRegression 模型?
我正在编写一个简单的程序,它从用户那里获取参数并处理它们.我在argv中的参数是二维数组.但是当我运行程序时,我得到垃圾值和分段错误错误.我已经尝试使用argc作为终止条件并且它有效.但我想只使用指针.指针在这里做错了什么.
#include<stdio.h>
int main( int argc, char *argv[])
{
while (++(*argv))
{
if ( **argv == '-' )
{
switch (*argv[1])
{
default:
printf("Unknown option -%c\n\n", (*argv)[1]);
break;
case 'h':
printf("\n option h is found");
break;
case 'v':
printf("option V is found");
break;
case 'd':
printf("\n option d is found");
break;
}
}
printf("\n outside while : %s", *argv);
}
}
Run Code Online (Sandbox Code Playgroud)
程序运行方式:
./a.out -h -v -d
Run Code Online (Sandbox Code Playgroud)
谢谢
我编写了一段代码来了解IPC和读写函数的基本知识.由于读取功能是阻塞的,因此读取将等待,直到其他进程将数据写入管道的另一端.我在父进程中的write()之前进行了sleep调用.在read()之前和之后的子进程中,我有打印时间.
#include <stdio.h>
int main()
{
int fd[2], err, pid;
FILE *fp;
char *buf;
buf = malloc(12);
fp = fopen("BE1.txt","w");
err = pipe(fd);
if(err == -1)
printf("Error while creating pipe");
pid = fork();
if(pid == -1)
printf("Error while creating process");
if(pid == 0)
{
fprintf(fp,"before read %s\n", __TIME__);
// fflush(fp);
read(fd[0], buf, 12);
fprintf(fp,"%s\n", buf);
// fflush(fp);
fprintf(fp,"after read %s\n", __TIME__);
}
else
{
sleep(50);
write(fd[1], "this is it", 12);
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于读取处于阻塞模式,子进程应在上面的代码中打印不同的时间.但它的打印时间相同
before read 19:48:16 …Run Code Online (Sandbox Code Playgroud)