- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在文件queue.h中有节点和队列结构
#ifndef QUEUE_H
#define QUEUE_H
#include<stdio.h>
#include<stdbool.h>
typedef struct rcb
{
int seq_no;
int file_desc;
FILE *fp;
long sent;
long sizeOfFile;
char *request_http_method;
char *request_file;
char *request_http_version;
int level;
bool is_new_created;
int remaining;
}* RCB;
/*
* Node of the queue.
*/
typedef struct node {
RCB info; //Data of each node.
struct node *ptr;//pointer to the next node.
} * Node;
/*
* Queue for the requests.
*/
typedef struct req_queue {
Node front; //front node.
Node rear; //rear node.
int size; //size of the queue.
} * Queue;
/**
* Create the queue.
*/
void create(Queue queue);
/**
* For inserting an item to the queue in sorted order of file size.
* @param data
*/
void enque_SJF(Queue queue, RCB data);
#endif
队列.c
#include "queue.h"
Node temp, front1;
int count = 0;
/**
* For creating the queue.
*/
void create(Queue queue) {
queue->front = queue->rear = NULL;
queue->size=0;
}
/**
* Enqueing in the order of increasing file size.
* @param data
*/
void enque_SJF(Queue que, RCB data) {
bool found = false;
Node temp = que->front;
while (!found) {
if (que->front == NULL) { //if the queue is empty.
que->front = malloc(sizeof (struct node));
que->front->ptr = NULL;
que->front->info = data;
que->rear = que->front;
break;
} else {
if (temp->ptr == NULL) {
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
if (temp->info->sizeOfFile >= data->sizeOfFile) {
newnode->ptr = temp;
que->front = newnode;
break;
} else { //else enqueue at the rear.
temp->ptr = newnode;
}
} else {
if (temp == que->front && temp->info->sizeOfFile >= data->sizeOfFile) {
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
newnode->ptr = temp;
que->front = newnode;
break;
}
if (temp->ptr->info->sizeOfFile >= data->sizeOfFile) {
Node newnode = (struct node *) malloc(1 * sizeof (struct node));
newnode->info = data;
newnode->ptr = temp->ptr;
temp->ptr = newnode;
break;
} else
temp = temp->ptr;
}
}
}
que->size++;
}
我正在尝试将一个新节点加入到queue.c 文件中的函数enque_SJF 中的队列中。 enqueue函数在sws.c中由函数serve_client函数调用。这是 sws.c 这些文件中还有更多函数,但它们与我的问题无关,因此我没有编写所有函数以使其更简单;
#include "Queue.h"
#include "network.h"
#include "schedulers.h"
#include "shared.h"
char scheduler[4];
pthread_t tid[2];
int port;
Queue req_queue;
bool flag[2];
int turn;
int sequence_number;
void *serve_client()
{
static char *buffer; /* request buffer */
int fd;
req_queue = (struct req_queue *) malloc( sizeof (struct req_queue));
create(req_queue);
if (port != 0)
{
network_init( port ); /* init network module */
fprintf(stderr, "Connection port %d\n", port);
for( ;; )
/* main loop */
{
network_wait();
if( !buffer ) /* 1st time, alloc buffer */
{
buffer = malloc( MAX_HTTP_SIZE );
if( !buffer ) /* error check */
{
perror( "Error while allocating memory" );
abort();
}
}
for( fd = network_open(); fd >= 0; fd = network_open() ) /* get clients */
{
memset( buffer, 0, MAX_HTTP_SIZE );
if( read( fd, buffer, MAX_HTTP_SIZE ) <= 0 ) /* read req from client */
{
perror( "Error while reading request" );
abort();
}
printf("file path %s\n",buffer);
//Initializing memory for the job.
RCB request = (RCB) malloc(1 * sizeof (struct rcb));
//breaking the request in appropriate format.
request-> request_http_method = strtok(buffer, " "); //request method.
request->request_file = strtok(NULL, " /"); //request file
request->request_http_version = strtok(NULL, "\n"); //HTTP version
request->file_desc = fd;
request->level = 1; // for multilevel scheduler.
request->seq_no = sequence_number;
sequence_number++; //increment global counter.
enque_SJF(req_queue, request); //Enqueue for Shortest Job First.
}
}
}
return 0;
}
bool isValidRequest(RCB request)
{
// the request is parsed and checked the validity
}
void *SJF( )
{
// function implemented
}
int main(int argc, char **argv )
{
//default port, if no port is supplied.
/* check for and process parameters
*/
if( ( argc < 3 ) || ( sscanf( argv[1], "%d", &port ) < 1 ) || ( sscanf( argv[2], "%s", scheduler ) < 1 ) )
{
printf("port %d\n",port);
printf("port %s\n",scheduler);
printf( "usage: sms <port> <scheduler>\n" );
return 0;
}
sequence_number = 1; //counter for number of requests.
if (argc == 3)
{
port = atoi(argv[1]);
}
printf("port %d\n",port);
pthread_create(&(tid[0]), NULL, serve_client, NULL);
if(strcmp(scheduler,"SJF") ==0)
{
pthread_create(&(tid[1]), NULL, SJF, NULL);
}
else if(strcmp(scheduler,"RR")==0)
{
pthread_create(&(tid[1]), NULL, Round_Robin, NULL);
}
else if(strcmp(scheduler,"MLFB")==0)
{
pthread_create(&(tid[1]), NULL, MultilevelQueueWithFeedback, NULL);
}
else
{
printf("Scheduler Algorithm is not defined. Please enter one of them; SJF, RR, MLFB");
return 0;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
在向队列添加新节点时,我在以下行收到段错误错误;
que->front->ptr = NULL;
在调试时,我发现内存分配后,que->front的地址仍然是0x0。有什么建议为什么它不分配内存吗?
最佳答案
这就是为什么您应该始终对被调用的函数执行成功检查。
如果malloc()
失败,它将返回一个NULL
指针,该指针存储在que->front
中。之后,如果您尝试访问
NULL
检查
que->front->ptr
您将取消引用NULL
指针(即访问无效内存),该指针会调用undefined behavior .
始终对 malloc()
的返回值进行 NULL 检查。
关于c - 在 C 中为队列的前面元素分配内存时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37936304/
很高兴和你分享一些有关写python代码的经验,简直就像是品尝一口甜甜的草莓冰淇淋。每当我开始写python代码的时候,就仿佛是开启了一段奇妙的冒险,每一行代码都是我的剧情发展,每一个bug都是我的挑
我有一张看起来像这样的 table , Date Value 01/01/2010 03:59:00 324.44 01/02/2010 09:31:00
我已尽我所能检查但未找到任何 kwds允许您在 y=a-x 上画一条线(例如 pandas )散点图(不一定是最佳拟合线)并将其带到后面(或前面)。 #the data frame ax=df.plo
我已尽我所能检查但未找到任何 kwds允许您在 y=a-x 上画一条线(例如 pandas )散点图(不一定是最佳拟合线)并将其带到后面(或前面)。 #the data frame ax=df.plo
我正在尝试在书签中使用 jquery UI 作为 slider 。并且 jquery ui 要求在普通 jquery 文件之后包含该文件。 所以到目前为止我所尝试的只是将脚本附加到 header ,同
我正在尝试将 div (.portrait_text) 添加到容器 (.gallery_container) 中: 每个图像都会添加到其相应的 .gallery_container 前面。但我也想将
因此,为了避免让自己头疼(反而制造头疼),我试图将一个字符串数组成员放在同名的结构变量前面,但它不起作用并给我一个错误:std::string {aka class std::basic_string
嗨,我的索引页面上的下拉菜单有问题,下拉元素隐藏在我的图像 slider 下方,它位于我的导航栏正下方,我希望能够看到下拉菜单元素,任何帮助将不胜感激非常感谢; 这是我的导航栏 html:
我想在 div 前面移动一个按钮。 http://demo.gbaus.com/index.html在网站中,获取报价按钮不可点击,除非您点击最底部。我希望将它移到前面,以便您可以单击它。 posot
我有一个以时隙为键的 map ,指向分配的客户。并非所有时隙都有指定的客户,它可能既稀疏又密集,所以我坚持使用 map。执行。如果存在分配,则仅存在键。 计数器从槽 1 计数到槽 x,并在每个槽检查分
我有三个 Controller 。一个是 HomeViewController 启动 BlahPageViewController 并且它是单独的页面。其他两个 Controller (BlahPag
我有一个文本区域。我可以设置它的文本 $("#mytextarea").text("foo") 我可以像这样添加到文本区域: $("#mytextarea").prepend("foo") 但是我不能
我喜欢在绘图时生成我自己的网格线,这样我就可以控制刻度线等,我正在用“hist”绘图程序来解决这个问题。 hist(WindSpeed, breaks=c(0:31), freq=TRUE,
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
当我用 Java 发送电子邮件时,如果单词“From”是任何行的第一个单词,则在该单词的开头附加一个“>”字符,我该如何阻止这种情况? 这是我的文字: The following transactio
我有这一行输出 span 标记的名称值: $(this).text($(this).attr("name")); 在“名称”值之前添加字符(特别是美元符号 ($))的最佳方式是什么。 最佳答案 尝试:
在我调用 segue 返回到以前的 View Controller 之一后,我一直在努力解决我的选项卡栏被隐藏的问题。 我读到,在segue之后, View 覆盖了标签栏。有没有办法将标签栏放在 Vi
我的目标是有一个窗口,其中背景有 2 个不同颜色的面板。它们各自覆盖屏幕的特定百分比,并且会定期变化。我通过创建一个 JSplitPane 来做到这一点。但现在我想添加一个 JLabel 在屏幕中间的
我试图让我的导航栏在滚动时出现在正文的顶部。目前它在下面,如图所示。 我还有一个抽屉导航,它在引入 ScrollView 后就停止工作了……我觉得这可能是因为当我单击“汉堡包”图标时,它实际上位于 S
我之前问过类似的问题,但可能不清楚。 我有一个在 Amazon Elastic beanstalk 上运行的 Web 服务 (tomcat 8),它运行良好。然后我在 DomainA.com 上运行网
我是一名优秀的程序员,十分优秀!