|
Hi, I am having a hard time understanding some basic concepts in real time systems. I have tried reading up on it but when viewing a concrete system I can't really understand it.
The application prints the time, student name, student number and semester year as given in the setup in the program runs a certain amount of loops for all tasks.
Scheduler set up:
Name Delay
Thread#1 95ms
Thread#3 187ms
Thread#5 463ms
When code is executed it looks like this.
Print Out:
[T1]:11:30:45:808[T3]:11:30:45:825[T5]:11:30:45:825[T1]:Mark Johnson[T1]:152585[T3]:Mark Johnson[T1]:2020
[T5]:Mark Johnson[T1]:2020
[T5]:Mark Johnson[T1]:11:30:45:949[T3]:152585[T1]:Mark Johnson[T1]:2020
[T3]:2020[T5]:152585[T1]:2020[T3]:2020
What are the real time requirements of this application?
|
|
|
|
|
Windows is not and never has been a real time system: it's a multithreaded preemptive scheduling system, and that's different.
Thread delays aren't absolute values, they are minimum values: the thread will delay for at least Xms, and will be available to run from that point - but that doesn't mean it will run immediately, or even soon, depending on other threads in the system and the number of cores available to process them.
As the "Threads : Cores" ratio rises, more and more threads get delayed more and more.
You have no direct control at all over when a thread will run: all you can do is say "wait at least this long"; you can't even guarantee that a thread will complete an operation before a second thread gets in there - so when you are sharing a single resource (such as the console) between multiple threads, you can't even guarantee that each message you see will be complete, much less in an order you want!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Original Griff, the question here is to analyze the system based on real-time requirements. The requirements will be any factor that may affect the running time of the tasks.The real-time involve tasks running in parallel, any common resources used by the system that will affect the timing and the time used by the application.
You are saying here that windows is not a real time system, but this is an app created to simulate a real time system that has some tasks running. That's a different thing inst it ?
If one disregards the CPU and computer memory which other resources are these tasks sharing when running?
|
|
|
|
|
What do you think?
It's your homework after all, not mine!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
"Real-time" is like an ATM; you want your cash NOW.
Anything other than "real-time", means stuff might go into a "queue" for a while before it get processed, if there is "other" stuff that also needs doing.
As for "your" app, no one knows what it's doing so no one can tell you if it has a "real-time" requirement or not.
And simply printing the same static information over and over isn't anything; it's just nonsense.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
auting82 wrote: What are the real time requirements of this application? There are not any real time requirements, since all you are doing is printing some general information about people in your database.
|
|
|
|
|
Hi
this is shyam from hyderabad city
request to provide the c# code related to multiple csv file into datatable or database
of mssql database
thanks in advance
|
|
|
|
|
Sorry, these forums are not suited for code requests. There exist some sites where you can rent coders, but CP isn't one of them.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I have a Web API to send SMS to customers. Web API is up and running 24/7/365. Web API is running fine without any issues.
However, I am invoking the API everytime through PostMan tool. In order to avoid this manual intervention, I have written a console application, but that console application sometimes works, sometimes didn't. it stops abruptly without any reason, I tried to catch error, but no effect. The application has to run automatically with an interval of 5 minutes in day. Every 5 minute it has to run. If already previous task is running, then no need to start a new task. I have implemented this by using a ProcessMap table in Sql DB. So each time it starts, it checks if any sms task is running. if running,it will not start new, otherwise starts a new task.
My question is , is this the correct way to invoke the API automatically or is there any other better way to do this? Kindly hep.
|
|
|
|
|
Given your requirements, I'd be inclined to write a console application to call the API once and then exit. Then use the Windows Task Scheduler to schedule that application to run every five minutes.
By default, if the task is already running, Task Scheduler won't start it again, so you shouldn't need any SQL tables or additional checks.
As for fixing the crashes, you'll need to debug your application. If you want someone to help you fix the errors, we'll need to see the full error details, and the relevant parts of your code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you Richard. I think if you can clarify little bit on first portion of your reply, I will be able to fix my issue.
In my console application, I have invoked the API as below:
class Program
{
static void Main(string[] args)
{
RunAsync().GetAwaiter().GetResult();
Environment.Exit(0);
}
static async Task RunAsync()
{
string httpReasonStatus = string.Empty;
int reasoncode;
using (var client = new HttpClient())
{
string baseURL = ConfigurationManager.AppSettings["APIURL"].ToString().Trim();
client.BaseAddress = new Uri(baseURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Get");
HttpResponseMessage response = await client.GetAsync("/api/SendBulkSMS");
if (response.IsSuccessStatusCode)
{
reasoncode = (int)response.StatusCode;
httpReasonStatus = response.ReasonPhrase;
}
else
{
reasoncode = 0;
httpReasonStatus = "error";
}
}
}
}
How would you call the Console application once, and then use the Windows Scheduler to schedule the application without using Sql tables. I am also not in favour of using Sql tables, and looking for any other better ways. Can you please provide any guide on this? Thanks a lot for the support.
|
|
|
|
|
That code is already doing what I expected - it calls the API once and then exits. There's no code to check a SQL table to try to enforce a single instance.
The task scheduler hasn't changed very much since Vista. You just need to launch it and create a new task to run your console application:
How to Automatically Run Programs and Set Reminders With the Windows Task Scheduler[^]
NB: I'd be inclined to make a couple of changes to your code:
static class Program
{
static async Task<int> Main()
{
string baseURL = ConfigurationManager.AppSettings["APIURL"].Trim();
if (!Uri.TryCreate(baseURL, UriKind.Absolute, out var baseAddress))
{
Console.Error.WriteLine("Invalid API URL: '{0}'", baseURL);
return -1;
}
using (var client = new HttpClient())
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Get");
using (var response = await client.GetAsync("/api/SendBulkSMS"))
{
Console.WriteLine("{0}: {1}", response.StatusCode, response.ReasonPhrase);
if (response.IsSuccessStatusCode) return 0;
return (int)response.StatusCode;
}
}
}
} - Since C# 7.1, you can make the
Main method async . - You can return the exit code directly, rather than using
Environment.Exit . - You should avoid throwing an exception if the URL in the config file is invalid.
- You should wrap the
HttpResponseMessage in a using block. - There's no point in the
reasoncode and httpReasonStatus variables, since you never use them. - Console applications should generally return
0 if they succeed, and non-zero if they fail. The non-successful HTTP status code looks like a good candidate here.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
<pre> Im creating a Xamarin.Android app and for database im using sqlite-net. I was trying to create a table with this structure
public class Templates
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
[TextBlob("imagesBlobbed")]
public List<int> Images { get; set; }
public string imagesBlobbed { get; set; }
}
but i want every time im adding a template to check if it already exist by checking the Images like this:
public static void AddTemplate(SQLiteConnection db, int category, List<int> images)
{
var alreadyExist = db.Table<templates>().Where(record => record.Images == images);
if (alreadyExist?.Count() == 0)
{
var template = new Templates()
{
Category = category,
Images = images,
};
db.InsertWithChildren(template, recursive: true);
}
}
but it's not working and i'm guessing it is not working because you cannot query with parameter "Images", in the database is stored as a blob. So im guessing i have to convert List to something else that is more easily supported in sqlite.
I'm thinking of using sqlite-net-extensions that support one to many relations and do something like this:
[Table("Templates")]
public class Template
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
[OneToMany]
public TemplateImage Images { get; set; } = new List<int>();
}
[Table("TemplateImages")]
public class TemplateImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Image1 { get; set; }
public int Image2 { get; set; }
public int Image3 { get; set; }
public int Image4 { get; set; }
public int Image5 { get; set; }
[ForeignKey(typeof(Template))]
public int TemplateId { get; set; }
}
In my application every template has 5 images, but this might change in the future. What is the correct approach according to DB design, to have a TemplateImage object with 5 images or to have only one image for every TemplateImage object like this:
[Table("Templates")]
public class Template
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
[OneToMany]
public List<templateimage> Images { get; set; } = new List<int>();
}
[Table("TemplateImages")]
public class TemplateImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Image { get; set; }
[ForeignKey(typeof(Template))]
public int TemplateId { get; set; }
}
Is the second approach slower? Is there any other better way for this?
|
|
|
|
|
Exoskeletor wrote: but it's not working and i'm guessing it is not working because you cannot query with parameter "Images", in the database is stored as a blob. So im guessing i have to convert List to something else that is more easily supported in sqlite. Comparing blobs isn't very efficient; make a hash-value of the blob (like md5), store that with the blob, and compare it using that. A hash-value would be like a fingerprint of your blob; it will change as soon as the image changes, but will result in the same values for the same images.
Exoskeletor wrote: In my application every template has 5 images, but this might change in the future. What is the correct approach according to DB design, to have a TemplateImage object with 5 images or to have only one image for every TemplateImage object like this: I'd have a table for the images; one field for the image, another field that holds the id of the template. That way you can have any number of images linked to the template.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Wow amazing response, thank you for your fast reply.
Im thinking if it is overkill now to create a Table for the images rather than using the blob but i like the idea of table for the images because it feels right in terms of database design.
However if i have a table for the images (so if i have a List<templateimage>) then i will not have a blob of a List<int> to make a md5 hash of it right? In that case if i want to check if a template with the same images exist i would have to retrieve all the images and check them one by one. If there is a way to md5 the List<templateimage> it would be the best i think.
I found also this c# - Create Hash Value on a List? - Stack Overflow[^] maybe i can use this to hash a List<objects>?
|
|
|
|
|
Exoskeletor wrote: i would have to retrieve all the images and check them one by one you would retrieve all the hashes, not the images, compare the hashes, not the images. You would only get the images if you need to display them, everything else would use the hashes.
You could also use the database to compare the hash using a where clause on your sql query.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Isn't it faster to hash the whole list<images> instead of comparing 5 hashes with a set of other 5 hashes?
|
|
|
|
|
Yes, but the business case would need to state that all 5 images must be identical.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
You mean that it is better in terms of programming design to manually check each image and not the list of images?
images are stored by their resource id which is unique, i could use this as a hash right?
|
|
|
|
|
i would need 5 queries to check if the 5 images exist in the DB, this is not expensive?
|
|
|
|
|
This decision cannot be driven by programming requirements but is a use/business case requirement. Do you need to know if all of the 5 images are identical between records or do you need to know if one of the 5 images is different. Does the sequence of images impact on the differences of the group.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
i prefer to know if all of them are identical, sequence is important when the app is running ,im getting them in the sequence i want with this code:
public static int GetSequenceHashCode<T>(this IList<T> sequence)
{
const int seed = 487;
const int modifier = 31;
unchecked
{
return sequence.Aggregate(seed, (current, item) =>
(current * modifier) + item.GetHashCode());
}
}
public static void AddTemplate(int category, List<int> images)
{
var tmpl = new Template()
{
Category = category,
};
var img1 = new TemplateImage()
{
Category = category,
Image = images[0],
};
var img2 = new TemplateImage()
{
Category = category,
Image = images[1],
};
var img3 = new TemplateImage()
{
Category = category,
Image = images[2],
};
var img4 = new TemplateImage()
{
Category = category,
Image = images[3],
};
var img5 = new TemplateImage()
{
Category = category,
Image = images[4],
};
tmpl.TemplateImages = new List<TemplateImage>() { img1, img2, img3, img4, img5 };
tmpl.ImagesHash = tmpl.TemplateImages.GetSequenceHashCode();
var result = DatabaseHelper.db().Query<Template>("Select * From Templates where ImagesHash=?", tmpl.ImagesHash).ToList();
if (result.Count == 0)
{
DatabaseHelper.db().InsertAll(tmpl.TemplateImages);
DatabaseHelper.db().Insert(tmpl);
DatabaseHelper.db().UpdateWithChildren(tmpl);
var employeeStored = DatabaseHelper.db().GetWithChildren<Template>(tmpl.Id);
}
however the GetSequenceHashCode doesnt work as expected, it gives different results on every run, so i will have to check with another code.
For the current state of the app if only one image is found is enough but i might need in the future to know if all of the images are the same (and exist all of them in one template).
I can get what i want with this(check if all images exist i mean):
var result = DatabaseHelper.db().Query<TemplateImage>("Select * from TemplateImages where Image=?", images[0]).Count +
DatabaseHelper.db().Query<TemplateImage>("Select * from TemplateImages where Image=?", images[1]).Count +
DatabaseHelper.db().Query<TemplateImage>("Select * from TemplateImages where Image=?", images[2]).Count +
DatabaseHelper.db().Query<TemplateImage>("Select * from TemplateImages where Image=?", images[3]).Count +
DatabaseHelper.db().Query<TemplateImage>("Select * from TemplateImages where Image=?", images[4]).Count;
;
if (result.Count == 5)
But looks very bad to me, will this have impact on performance? is there any other way for the same result?
modified 8-Mar-20 22:23pm.
|
|
|
|
|
Exoskeletor wrote: however the GetSequenceHashCode doesnt work as expected, it gives different results on every run, so i will have to check with another code. Try the md5 hash; it will be consistent between runs.
Exoskeletor wrote: But looks very bad to me, will this have impact on performance? is there any other way for the same result? You could combine those five queries into one. Something like "SELECT Id FROM TemplateImages WHERE ImageHash IN (@value1, @value2, @value3, @value4, @value5". If the values already exist in the table, such a query would give you their Id's.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
The blob (the image). There's more than one way to create a hash, and there are different algorithms that generate different hashes. MS recommends using SHA256, but the idea is the same. See MD5 Class (System.Security.Cryptography) | Microsoft Docs[^]
Once you can do that, you can save the hash with the image. If you want to check whether the image is already in the database, you hash your image and query the database to see if it is already there.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|