我想创建一个简单的程序,该fork,并且子进入命名管道,父进程从命名管道读取和显示.问题是它进入了父进程,进行了第一次printf然后它变得奇怪,它没有做任何其他事情,没有进入第二个printf,它只是在控制台中输入的方式.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
char t[100];
mkfifo("myfifo",777);
pid_t pid;
pid = fork();
if (pid==0)
{
//execl("fifo2","fifo2",(char*)0);
char r[100];
printf("scrie2->");
scanf("%s",r);
int fp;
fp = open("myfifo",O_WRONLY);
write(fp,r,99);
close(fp);
printf("exit kid \n");
exit(0);
} else
{
wait(0);
printf("entered parent \n"); // <- this it prints
// whats below this line apparently its not being executed
int fz; printf("1");
fz = open("myfifo",O_RDONLY); printf("2");
printf("fd: %d",fz);
char p[100];
int size; …Run Code Online (Sandbox Code Playgroud) 您将如何使用C中的链接列表实现优先级队列?
典型的链表包括head指向指向另一个元素的元素,该元素最终以NULL链接列表的尾部结尾.例:
(Linked List | Head) ----> (Element | Next) ----> (Element | Next) ----> Null
Run Code Online (Sandbox Code Playgroud)
在基本场景中,通过使用先进先出(添加到列表末尾,从列表前面删除)FIFO方法将新元素添加到列表中.
但是,在我的情况下,必须考虑优先级值.更具体地说,每个元素可以被赋予1,2或3的优先级.具有最高优先级的元素被添加到列表的前面,而具有较低优先级的元素被添加到后面.插入列表会保持每个优先级的FIFO顺序.
因此,如果要一次将一个元素排入队列:
a 3, b 1, c 2, d 3, e 2
Run Code Online (Sandbox Code Playgroud)
输出应该是:( a 3, d 3, c 2, e 2, b 1按优先级排序,以及添加的顺序,而不是标准的先入先出方法,忽略优先级).
这是我所拥有的,但它没有优先权.您将如何实施优先级队列?
一种方法是使用排序/优先级算法.除了算法之外,对我来说,一些主要的未知数/混淆是如何以及在何处存储优先级,它是否在实际元素中,例如:
(Linked List | Head)---->(a | 1 | Next)---->(b | 2 | Next)----> Null
要么
q_enqueue(&q, "a", "1");
q_enqueue(&q, "b", "2");
Run Code Online (Sandbox Code Playgroud)
在使用指针创建排序算法时,我将如何比较优先级.
我正在创建一个实现为双向链表的 FIFO,但我不知道为什么我会收到递归错误。我已经发布了我的代码和我在下面收到的错误。任何帮助将非常感激!
"""DLList.py"""
class DLList(object):
"""A doubly-linked list
attributes: nl = node list"""
def __init__(self, nl=[]):
self.nl = nl
def __str__(self):
return str(self.nl)
def __len__(self):
return len(self.nl)
def append(self, node):
if not isinstance(node, DLNode):
raise TypeError
try:
node.pn = self.nl[-1]
node.pn.nn = node
self.nl.append(node)
except:
self.nl.append(node)
def pop(self):
rn = self.nl.pop(0)
try:
self.nl[0].pn = None
except:
pass
return rn
class DLNode(object):
"""A node in a doubly-linked list.
attributes: self.pn = previous node, self.data = data, self.nn = next node"""
def …Run Code Online (Sandbox Code Playgroud) 我在寻找简单的字节FiFo缓冲区.我必须放置或获取或单个字节或数组.但我可以每次都将单字节放入并获得数组,反之亦然.
任何想法或示例代码来帮助我?
在Scala项目中,我需要一个简单的,可变的队列数据结构,我可以在一端附加项目并在另一端取出项目(即FIFO).现在,我不确定是否应该使用LinkedListJava中的普通旧版本或Scala版本DoubleLinkedList.这两者的相对优势是什么?我应该总是喜欢DoubleLinkedList,还是有充分的理由使用LinkedList?此外,还有其他值得考虑的选择吗?
我需要使用单个文件运行 mpg123,这样它会像往常一样自动启动和自动关闭,但是,我需要能够使用发送到 fifo 文件的命令来覆盖此默认行为。
我一直在运行mpg123 filename.mp3脚本,只是等待它完成后再继续。但是,我希望另一个脚本能够根据用户的输入暂停播放、控制音量或提前终止进程。
mpg123 -R --fifo /srv/http/newsctl filename.mp3 似乎启动 mpg123 并创建管道,但不开始播放。
我如何使这项工作?
我需要一个丢弃FIFO队列,它会在项目满时自动丢弃.它不必是线程安全的.效率更重要.
也就是说,我需要从设备中采样信号,同时能够在半随机时间检索最后n秒(对象).
我自己实现了一个(不是那么线程安全的)缓冲区,但感觉我在这里重新发明了这个轮子.另外我们每秒都在讨论100个物体.大多数将被丢弃,而例如每次必须检索3000(= 30秒的数据)(例如每十分钟).
Python标准库或其他地方是否已有这样的类?我已经使用了一些goggle-fu但没有找到任何有用的东西.
from Queue import Queue, Full, Empty
import logging
class DiscardingBuffer():
def __init__(self, capacity=0):
self._queue = Queue(maxsize=capacity)
def put(self, item):
while True:
try:
self._queue.put(item, block=False)
logging.debug('Put item: {0}'.format(item))
break
except Full:
discarded_item = self._queue.get(block=False)
logging.debug('Buffer is full. Discarding: {0}'.format(discarded_item))
def flush(self):
items = []
while True:
try:
items.append(self._queue.get(block=False))
except Empty:
logging.debug('Buffer is now empty.')
break
return items
def main():
buf = DiscardingBuffer(5)
for i in xrange(10):
buf.put(i)
logging.debug('Remaining items: {0}'.format(buf.flush()))
logging.debug('Verify …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试用C语言编写一个程序,该程序将从两个命名管道中读取并在stdout可用时将任何数据打印到stdout.
例如:如果我打开两个终端并./execute pipe1 pipe2在其中一个终端(管道1和管道2是有效的命名管道)然后键入管道echo "Data here." > pipe1的名称(这里是pipe1),大小和数据应打印到stdout- - 这里看起来像pipe1 [25]: Data here.
我知道我需要用O_RDONLY和O_NONBLOCK旗子打开管道.我已经看过许多人使用的例子(在这个论坛上有很多),select()我仍然不明白传递的不同参数select()是做什么的.如果有人可以在这里提供指导,那将非常有帮助.下面是我到目前为止的代码.
int pipeRouter(char[] fifo1, char[] fifo2){
fileDescriptor1 = open(fifo1, O_RDONLY, O_NONBLOCK);
fileDescriptor2 = open(fifo2, O_RDONLY, O_NONBLOCK);
if(fileDescriptor1 < 0){
printf("%s does not exist", fifo1);
}
if(fileDescriptor2 < 0){
printf("%s does not exist", fifo2);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个 FIFO 和两个线程。一个线程只会从 FIFO 入队,而另一个线程只会从 FIFO 出队。我需要使用 ConcurrentQueue 还是 Queue 就足够了?
我想知道 golang 频道中元素的顺序。运行几个例子后,元素离开通道的顺序似乎是“后进先出”。我对吗?
以下代码段是我使用的示例。运行代码后,输出为 20 10,其中 10 首先发送到通道,20 最后发送到通道。
package main
import "fmt"
func multiply(c chan int, num int) {
c <- num * 10
}
func main() {
c := make(chan int)
go multiply(c, 1)
go multiply(c, 2)
v1 := <-c
v2 := <-c
fmt.Println(v1, v2)
}
Run Code Online (Sandbox Code Playgroud)